2017-09-15 73 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"/> 
相關問題