2016-07-31 263 views
0

我正在使用RelativeLayout用於顯示一個按鈕或兩個按鈕。對於兩個按鈕的情況,它們需要左/右對齊,對於一個按鈕的情況,它需要居中。RelativeLayout,對齊左/右兩個按鈕,對齊中心爲一個

<RelativeLayout> 
    <Button 
    android:id="@+id/action" 
    android:layout_alignParentLeft="true"/> 
    <Button 
    android:id="@+id/dismiss" 
    android:layout_alignParentRight="true"/> 
</RelativeLayout> 

我改變了解僱按鈕View.GONE一個按鈕時,不過動作按鈕仍然對齊到左 - 是否有任何非的程序化的方式來對齊中心?

+0

你能發佈完整的xml –

回答

1
<LinearLayout 
    android:orientation="horizontal"> 
    <Button 
    android:id="@+id/action" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"/> 
    <Button 
    android:id="@+id/dismiss" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 
</LinearLayout> 
+0

謝謝!我用寬度wrap_content,它也可以 –