2017-09-15 72 views
-2

我试图让我的Textview对齐到左侧尝试过。和android:gravity="left"但没有发生。在LinearLayout中对齐左侧的textview(水平)

这是我的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> 
+0

尝试安卓layout_gravity = “左” –

回答

0

尝试这种使用RelativeLayout insted的的LinearLayout,使您的TextViewandroid: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> 
1

gravity属性是用来设置的内容的严重性View

layout_gravity属性用于设置视图本身与其父项的重力。

所以,如果你想设置TextView中的文本的严重性,那么你会使用gravity。如果你想在LinearLayout内设置TextView的重力,你应该使用layout_gravity

2

试试这个使用代码:

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

如果你想使用线性布局,然后使用:

<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"/> 
相关问题