android: 如何使视图填充可用 space

android: How to make view fill available space

我真的找不到这个问题的答案,我找到的只是本地化的问题。假设我有这个:

我可以成功做到这一点。但是,我希望如果“BUTTON2”的可见性设置为 GONE,则使 BUTTON1 的宽度尽可能多。所以它看起来像这样:

目前这里的非工作代码:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_gravity="center">

            <com.rey.material.widget.Button
                android:id="@+id/button2"
                style="@style/Material.Drawable.Ripple.Wave.Light"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="goToOccasion"
                android:padding="20dp"
                android:text="@string/join"
                app:rd_enable="true"
                app:rd_rippleColor="@android:color/darker_gray"
                app:rd_rippleType="wave"
                android:gravity="center"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/joinOccasionBTN"
                android:layout_marginStart="87dp"
                android:layout_alignBottom="@+id/joinOccasionBTN" />

            <com.rey.material.widget.Button
                android:id="@+id/joinOccasionBTN"
                style="@style/Material.Drawable.Ripple.Wave.Light"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="goToOccasion"
                android:padding="20dp"
                android:text="@string/join"
                app:rd_enable="true"
                app:rd_rippleColor="@android:color/darker_gray"
                app:rd_rippleType="wave"
                android:gravity="center"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true" />
        </RelativeLayout>

结果是:

当 button2 为 visible 时,结果与上图相同,但没有 button2 ("JOIN")。

不使用 relativeLayout,而是使用 gridView 并在其中添加您的按钮。 如果您在列上设置,您的按钮将占据所有位置。 如果您设置两列,gridview 会自动将您的视图调整为两个大小合适的列...

两列:

<GridView
    android:layout_width="match_parent"
    android:numColumns="2"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</GridView>

一栏:

<GridView
    android:layout_width="match_parent"
    android:numColumns="1"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <Button
        android:id="@+id/button2"
        android:visibility:"gone"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</GridView>

希望这对您有所帮助。

一个简单的方法是在 LinearLayout.

中使用 android:layout_weight 属性
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        android:layout_gravity="center">

   <com.rey.material.widget.Button
       android:layout_width=0dp
       android:layout_height="wrap_content"
       android:layout_weight=1
       ...
       />

    <com.rey.material.widget.Button
       android:layout_width=0dp
       android:layout_height="wrap_content"
       android:layout_weight=1
       ...
       />

</LinearLayout>

这样,如果您的一个按钮的可见性从 visible 变为 gone,那么另一个按钮将填充剩余的 space。