2010-04-12 115 views
2

我想知道如何獲得這樣的結果(在圖片鏈接):如何做到這一點佈局?

http://img718.imageshack.us/img718/6173/screenshot20100411at126.png

這是我的嘗試:

<RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="#CCCCCC" 
        android:orientation="horizontal" 
        android:layout_weight="0"> 

     <Button android:id="@+id/btn_cancel" 
       android:text="@string/cancel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="5px" 
       android:gravity="left" 
       /> 

     <Button android:id="@+id/btn_save" 
       android:text="@string/save" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="5px" 
       android:layout_toRightOf="@id/btn_cancel" 
       android:gravity="right"/> 
    </RelativeLayout> 

我試圖與layout_width性能發揮和設置起來以填補他們兩個人的父母,但沒有工作,如果任何人有想法...謝謝!

+1

你的問題在這裏回答:http://stackoverflow.com/questions/2127459/which-android-control-to-use – 2010-04-12 06:38:13

+0

好吧,我去檢查一下! – Spredzy 2010-04-12 07:39:30

回答

4
<LinearLayout android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="5dip" 
       android:background="#CCCCCC" 
       android:orientation="horizontal"> 

    <Button android:id="@+id/btn_cancel" 
      android:text="@string/cancel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dip" 
      android:layout_weight="1"/> 

    <Button android:id="@+id/btn_save" 
      android:text="@string/save" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 
</LinearLayout> 

在LinearLayout中的手段來得到兩個widget佔用的空間相同數量是設置在兩者layout_width="fill_parent",然後將他們兩個layout_weight="1"爲好。設置爲fill_parent和相同的layout_weight將告訴LinearLayout分離兩者之間的所有可用空間。

此外,使用dip,而不是pxdip獨立於屏幕尺寸,並且在不同尺寸的屏幕上看起來會更好。

+0

謝謝你的代碼和下面的解釋。很高興知道。 – Spredzy 2010-04-12 07:39:03