Reputation: 648
In my activity I have LinearLayout with 4 elements:
<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>
When I have all 4 elements set the layout is rendered as following:
Which is as I want it. The problem occurs when one or more of the elements are not present and I get the following result:
How can I make my elements spread over all available space? So when only 2 elements have their background set the result should look something like:
Upvotes: 0
Views: 903
Reputation: 2812
you have to use weight so when no view will be there it will adjust automatically
<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>
Upvotes: 2
Reputation: 10299
Here android:gravity="center"
will align all the child view in 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>
Upvotes: 0