2016-07-31 217 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,它也可以 –