2010-09-29 41 views
0

我有4個水平線性佈局的按鈕。我想第四個按鈕的右邊緣與線性佈局的右邊緣對齊(等於屏幕寬度)我嘗試過使用android:layout_gravity =「right」,但它不起作用 - 右鍵是在第三個的右邊,但沒有對齊。我錯過了明顯的東西嗎?在main.xml中的android:layout_gravity效果?

main.xml中,只有相關的佈局僅params爲:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <Button 
      android:layout_height="fill_parent" 
      android:layout_width="wrap_content"> 
     <Button SAME LAYOUT AS BUTTON 1> 
     <Button SAME LAYOUT AS BUTTON 1> 

     <Button 
      android:layout_height="fill_parent" 
      android:layout_width="wrap_content" 
      android:layout_gravity="right" /> 
    </LinearLayout> 
</LinearLayout> 

這似乎並不匹配在SDK參考layout_gravity所陳述的行爲:「定義如何放置視圖,它的兩個x軸和y軸,在其父視圖組內。「

回答

3

我知道這不是你的問題的真正答案,但你應該真的考慮使用RelativeLayout而不是LinearLayout。它可以更好地適應不同的手機屏幕分辨率,並且可以使用XML中的layout_toRightOflayout_alignParentRight屬性(或layout_abovelayout_alignParentTop等)以正確的順序放置按鈕。在線性佈局中使用重力並不會像您期望的那樣運行,因爲Android仍試圖將小部件放置在固定位置。

RelativeLayout比LinearLayout工作起來更麻煩一些,但這很麻煩,也是Google推薦使用的。一旦你掌握了它,它們非常便於使用。

+0

Upvoted for great justice! – benvd 2010-09-29 15:06:25

+0

謝謝Nik。我認爲用RelativeLayout進行控制似乎有更多的空間。這個按鈕仍然有點跳躍,但我會堅持下去! – NickT 2010-09-29 18:52:36

+1

@NickT,保持它,並確保在每個控件上定義layout_align *以使它們正確對齊。有關RelativeLayout技巧的更多信息,請參閱:http://www.nikreiman.com/2010/09/android-relativelayout-ninja-fu-or-how.html – 2010-09-29 19:55:51