【问题标题】:TextView Center Gravity not workingTextView 中心重力不起作用
【发布时间】:2016-07-06 22:05:20
【问题描述】:

我已经尝试了所有我能想到的可能组合,但是我无法正确定位 textview。这是问题的屏幕截图

A符号图片的第一行可以清楚地看到,上边距大于下边距。

这是行的xml布局

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#191e1e">

    <RelativeLayout
        android:id="@+id/client_initial_layout"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerVertical="true"

        android:layout_marginLeft="10dp"
        android:background="#1f97e5">
        <!--  android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginTop="20dp"-->
        <TextView
            android:id="@+id/client_initial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:layout_centerHorizontal="true"

            android:text="X"
            android:textColor="#ffffff"
            android:textSize="60sp" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/lin_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/client_initial_layout"
        android:orientation="vertical">

        <TextView
            android:id="@+id/Client_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Guest"
            android:textColor="#bcc3c4"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_contacted"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Contacted:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/contacted"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Username"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_product"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Product:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/product"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Product Name"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_contact_type"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Contact Type:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/contact_type"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Dispute"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/lin_created"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginTop="10dp">

            <TextView
                android:id="@+id/text_Created_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Created Date:-"
                android:textColor="#bcc3c4"
                android:textSize="8sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/request_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" 2015-09-11-19:22:08"
                android:textColor="#bcc3c4"
                android:textSize="8sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="3dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="Request Date:-"
                    android:textColor="#bcc3c4"
                    android:textSize="8sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/create_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" 2015-09-11"
                    android:textColor="#bcc3c4"
                    android:textSize="8sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

我尝试了所有与中心相关的对齐方式,但没有效果。但是 xml 编辑器中显示的预览是完美的,并且在中心,但在我运行应用程序时不是,而且我也没有从 java 代码中设置任何类型的边距

【问题讨论】:

  • 您希望 productName 居中?
  • A 彩色框中心的符号
  • 将布局重心更改为中心
  • 在下面查看我的答案,这应该对你有用!@VivekMishra
  • 请检查您的片段正在替换的父布局的填充。

标签: android android-layout android-studio text-alignment layout-gravity


【解决方案1】:

改为Linearlayout,稍作改动就得到了这个结果,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#191e1e">

    <LinearLayout
        android:id="@+id/client_initial_layout"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:background="#1f97e5"
        android:gravity="center">

        <TextView
            android:id="@+id/client_initial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="X"
            android:textColor="#ffffff"
            android:textSize="60sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/Client_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Guest"
            android:textColor="#bcc3c4"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_contacted"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Contacted:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/contacted"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Username"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_product"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Product:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/product"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text=" Product Name"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp">

            <TextView
                android:id="@+id/text_contact_type"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Contact Type:-"
                android:textColor="#797878"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/contact_type"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=" Dispute"
                android:textColor="#797878"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/lin_created"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginTop="10dp">

            <TextView
                android:id="@+id/text_Created_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Created Date:-"
                android:textColor="#bcc3c4"
                android:textSize="8sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/request_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" 2015-09-11-19:22:08"
                android:textColor="#bcc3c4"
                android:textSize="8sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="3dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="Request Date:-"
                    android:textColor="#bcc3c4"
                    android:textSize="8sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/create_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" 2015-09-11"
                    android:textColor="#bcc3c4"
                    android:textSize="8sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

【讨论】:

  • 它显示我在中心。你没有得到什么?
  • 它在工作室的 xml 编辑器中显示在中心,但在我运行它时不显示
  • 好吧,您可能需要根据您的设备管理 android:textSize
  • 在没有任何效果之后,我现在使用带有左右填充的 wrap_content
  • textview 的 android:textSize 的减少对您不起作用?
【解决方案2】:

像下面这样改变你的相对布局,它会起作用

 <RelativeLayout
        android:id="@+id/client_initial_layout"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerVertical="true"

        android:layout_marginLeft="10dp"
        android:background="#1f97e5">
        <!--  android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginTop="20dp"-->
        <TextView
            android:id="@+id/client_initial"
            android:layout_width="70dp"
            android:layout_height="70dp"


       android:gravity="center"

            android:text="X"
            android:textColor="#ffffff"
            android:textSize="60sp" />
    </RelativeLayout>

希望这个答案对你有帮助

【讨论】:

  • 这会将文本向左移动
  • 它在我这边工作得很好,你可以试试 textview client_initial ,给出高度宽度 fill_parent
【解决方案3】:

试试中心垂直

<RelativeLayout
        android:id="@+id/client_initial_layout"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerVertical="true"

        android:layout_marginLeft="10dp"
        android:background="#1f97e5">
        <!--  android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginTop="20dp"-->
        <TextView
            android:id="@+id/client_initial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="X"
            android:textColor="#ffffff"
            android:textSize="60sp" />
    </RelativeLayout>

【讨论】:

    【解决方案4】:

    使用它:

     <RelativeLayout
        android:id="@+id/client_initial_layout"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerVertical="true"
    
        android:layout_marginLeft="10dp"
        android:background="#1f97e5">
        <!--  android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginTop="20dp"-->
        <TextView
            android:id="@+id/client_initial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="X"
            android:textColor="#ffffff"
            android:textSize="60sp" />
    </RelativeLayout>
    

    【讨论】:

      【解决方案5】:

      试试下面的代码

      说明:- 因为您已定义 widthheight,所以您的布局不会在布局中心显示文本,因此您必须将 layoutTextView 设为 @987654325 @ 并尝试将 padding 提供给您的 TextView,它应该可以工作!

      <RelativeLayout
                  android:id="@+id/client_initial_layout"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
      
      
                  android:layout_marginLeft="10dp"
                  android:background="#1f97e5">
      
                  <TextView
                      android:id="@+id/client_initial"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerInParent="true"
                      android:gravity="center"
                      android:paddingLeft="20dp"
                      android:paddingRight="20dp"
      
                      android:text="A"
                      android:textColor="#ffffff"
                      android:textSize="60sp" />
              </RelativeLayout>
      

      【讨论】:

      • 添加填充将是一种解决方法,而不是正确的解决方案
      • 如果你想给出特定的宽度高度我认为这是解决方案。@VivekMishra
      • 固定宽度高度会有什么问题?
      • 在android中具有固定的宽度/高度不会在中心显示小部件的文本,因为我也遇到过很多次。因此,如果您能找到任何解决方案,请在此处发布!
      猜你喜欢
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多