将文本视图在 LinearLayout 中向左对齐(水平)

Align a textview to left in a LinearLayout(horizontal)

我正在尝试使我的 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>

尝试使用 [=h11=] 而不是 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>

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

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

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

试试这个使用代码:

<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>

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

 <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"/>