0

好吧,我不会说一口流利的英语,所以很难解释我想要什么。

我在 Android 中有一个自定义对话框,带有 2Buttons

<LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="10dp">

        <Button android:id="@+id/button_ok"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginRight="5dp"
                android:background="@drawable/dialog_button_background_selector"
                style="@style/text_big_bold_inverted"
                android:text="@string/ok"/>

        <Button android:id="@+id/button_cancel"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginRight="5dp"
                android:background="@drawable/dialog_button_background_selector"
                style="@style/text_big_bold_inverted"
                android:text="@string/cancel"/>

    </LinearLayout>

现在有时我只需要 OKButton并且我使取消Button不可见。但是 OKButton停留在左边,Button当另一个不可见时,是否可以将其移动到中心?

我尝试使用.setGravity,但没有奏效。

4

2 回答 2

2

将取消按钮的可见性设置为 View.GONE 而不是 View.VISIBLE。

于 2012-09-06T10:50:48.103 回答
1

首先设置android:layout_gravity="center_horizontal"为您的 LinearLayout。

然后代替 View.INVISIBLE 使用 View.GONE 按钮。

button.setVisibility(View.GONE);
于 2012-09-06T10:50:39.893 回答