【问题标题】:Align a textview to left in a LinearLayout(horizontal)在 LinearLayout 中将文本视图左对齐(水平)
【发布时间】:2023-03-31 08:33:01
【问题描述】:

我正在尝试让我的Textview 与左对齐。还有android:gravity="left",但什么也没发生。

这是我的 XML 代码:

  <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
         <TextView
          android:id="@+id/textView11"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_margin="10dp"
          android:layout_weight="1"
          android:text="Email:"
          android:textSize="20dp"/>
        <EditText
          android:id="@+id/editTextAddress"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="10dp"
          android:layout_weight="1"
          android:background="@drawable/bg"
          android:hint="Enter Address"/>
  </LinearLayout>

【问题讨论】:

  • 试试 android:layout_gravity="left"

标签: java android xml android-layout android-xml


【解决方案1】:

试试这个使用代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<TextView
    android:id="@+id/textView11"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_margin="10dp"
    android:layout_weight="1"
    android:text="Email:"
    android:textSize="20dp"/>

<EditText
    android:id="@+id/editTextAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView11"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_toEndOf="@+id/textView11"
    android:layout_weight="1"
    android:background="@drawable/bg"
    android:hint="Enter Address"/></RelativeLayout>

【讨论】:

    【解决方案2】:

    gravity属性用于设置View内容的比重。

    layout_gravity 属性用于设置视图本身相对于其父级的重力。

    因此,如果您想在TextView 中设置文本的重心,那么您可以使用gravity。如果你想在LinearLayout 中设置TextView 的重力,你应该使用layout_gravity

    【讨论】:

      【解决方案3】:

      尝试使用RelativeLayout insted of LinearLayout 并制作您的TextView android:layout_alignParentLeft="true"

      <RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
          <TextView
              android:id="@+id/textView11"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:layout_weight="1"
              android:layout_alignParentLeft="true"
              android:text="Email:"
              android:textSize="20dp"/>
          <EditText
              android:id="@+id/editTextAddress"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:layout_weight="1"
              android:layout_alignParentRight="true"
              android:background="@drawable/bg"
              android:hint="Enter Address"/>
      </RelativeLayout>
      

      【讨论】:

        【解决方案4】:

        如果您想使用线性布局,请使用:

         <TextView
                android:id="@+id/textView11"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_alignParentLeft="true"
                android:text="Email:"
                android:textSize="20dp"/>
        

        【讨论】:

          猜你喜欢
          • 2017-08-30
          • 2017-09-22
          • 1970-01-01
          • 2015-10-09
          • 1970-01-01
          • 2015-07-02
          • 2014-01-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多