RelativeLayout,左/右对齐两个按钮,对齐一个中心

时间:2016-07-31 00:44:40

标签: android layout android-relativelayout

我正在使用 RelativeLayout 来显示一个按钮或两个按钮。对于两个按钮的情况,它们需要左/右对齐,对于一个按钮的情况,它需要居中。

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

我将关闭按钮更改为View.GONE一个按钮的情况,但操作按钮仍然左对齐 - 是否有任何非编程方式将其对齐?

1 个答案:

答案 0 :(得分: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>
相关问题