如何在未设置某些元素时使LinearLayout填充可用空间

时间:2015-11-23 12:45:36

标签: android android-layout android-linearlayout

在我的活动中,我有4个元素的LinearLayout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:id="@+id/linearLayout2">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgButton1"
        android:layout_weight=".25"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:background="@null" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgButton2"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_weight=".25"
        android:background="@null" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgButton3"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_weight=".25"
        android:background="@null" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgButton4"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_weight=".25"
        android:background="@null" />

</LinearLayout>

当我设置了所有4个元素时,布局呈现如下: Result when all 4 elements are set

这就是我想要的。当一个或多个元素不存在时会出现问题,我得到以下结果:

Result when only 3 elements are set

如何让我的元素遍布所有可用空间?因此,当只有2个元素的背景设置时,结果应如下所示: Expected result when 2 elements are set

而不是现在的样子: enter image description here

0 个答案:

没有答案