简体   繁体   中英

Align a textview to left in a LinearLayout(horizontal)

I'm trying to make my Textview aligned to left have tried. And android:gravity="left" but nothing happened.

This is my XML code:

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

Try this use code:

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

The gravity attribute is used to set the gravity of the content of the View .

The layout_gravity attribute is used to set the gravity of the view itself with regards to its parent.

So if you wanted to set the gravity of the text in your TextView then you'd use gravity . If you want to set the gravity of the TextView inside the LinearLayout you should use layout_gravity .

try this use RelativeLayout insted of LinearLayout and make your 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>

If you want to use Linear Layout then use :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM