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

How to make LinearLayout fill available space when some elements are not set

在我的 activity 中,我有包含 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 个元素后,布局呈现如下:

如我所愿。当一个或多个元素不存在时出现问题,我得到以下结果:

如何让我的元素分布在所有可用的元素中 space?因此,当只有 2 个元素设置了背景时,结果应该类似于:

而不是现在的样子:

你必须使用重量,所以当没有视图时它会自动调整

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

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

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

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

 </LinearLayout>

此处android:gravity="center"将对齐center,

中的所有子视图
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"    
    android:layout_alignParentBottom="true"
    android:id="@+id/linearLayout2">

   ... your code

</LinearLayout>