2010-04-26 104 views
2

我有這個XML代碼,它會生成一個按鈕,一個textview和另一個按鈕,我該如何去讓按鈕出現在最左邊, textview在中心和最右邊的最後一個按鈕?Android XML - 如何讓項目對齊很遠的左邊,中間和最右邊

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/LinearLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <Button android:id="@+id/Button01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Cancel"> 
    </Button> 
    <TextView android:id="@+id/TextView01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Place"> 
    </TextView> 
    <Button android:id="@+id/Button03" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Save"> 
    </Button> 

</LinearLayout> 

回答

2

您需要使用weight屬性。把它想象成讓android知道它應該給每個項目的寬度的百分比。

您需要設置寬度0dip所有3項,並添加重量屬性

<Button android:id="@+id/Button01" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Cancel" 
       android:layout_width="0dip" 
       android:layout_weight="2" > 
     </Button> 
     <TextView android:id="@+id/TextView01" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="New Place" 
       android:layout_width="0dip" 
       android:layout_weight="1" > 
     </TextView> 
     <Button android:id="@+id/Button03" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Save" 
       android:layout_width="0dip" 
       android:layout_weight="2" > 
     </Button> 

玩弄權重值,你就會明白它是如何工作:)

此之後,你可以使用重力屬性將文本移動到中心/左側或右側。

8

我會推薦使用RelativeLayout,它使事情變得更容易。

使用RelativeLayout的你就可以這樣做,你將能夠直接連接這些按鈕到遠邊他們對準父後設置TextView的父或centerin父

無論是中心水平或者直接將它們連接到textview的兩側,可能會添加一點填充,並且你已經獲得了空間。

1

如果你想保持LinearLayout,你需要做的是:創建一個TableRow,然後把所有的項目(包括按鈕和Textview)。這將創建一條整齊排列的所有項目的直線。此代碼適合我,對不起,我無法忍受它的一個截圖(新,並沒有足夠的聲望點),但它對我有用,我希望它可以幫助你。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TableRow android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_margin="5dp"> 

<Button android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Cancel" 
    android:layout_marginRight="30dp" 
    android:layout_marginLeft="10dp"> 
</Button> 

<TextView android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Place" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginRight="30dp"> 
</TextView> 

<Button android:id="@+id/Button03" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Save" 
    android:layout_gravity="right"> 
</Button> 
</TableRow> 
</LinearLayout>