android:如何使视图填充可用空间

时间:2016-09-22 15:52:01

标签: android android-layout android-view

我无法找到这个问题的答案,我所发现的只是本地化问题。假设我有这个:

image

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

image

目前这里是非工作代码:

<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>

结果如下:

image

当button2为visible时,它会产生与上图相同但没有button2(“JOIN”)的情况。

2 个答案:

答案 0 :(得分:3)

一种简单的方法是使用android:layout_weight中的LinearLayout属性。

<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,则另一个按钮将填充剩余空间。

答案 1 :(得分:1)

使用gridView并在其中添加按钮,而不是relativeLayout。 如果您设置了列,您的按钮将占据所有位置。 如果您设置两列,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>
希望这会有所帮助。

相关问题