In the image below, the yellow square represents a RelativeLayout that’s within my overall layout.
The top row “status message” is a ViewFlipper that responds to the ToggleButtons (A, B) that the user can press. Buttons C, D, and E do other stuff that reload the entire view. Our client is requesting that buttons A, B, C, D, and E be arranged as in the fashion below. (Vertical alignment isn’t as important as horizontal alignment.)
EDIT to say A, B, C, D, and E are images about 20×20 dip; they are being aligned within a width of about 300dip. I want the buttons to maintain their aspect ratio.

I’ve created an extension of LinearLayout that inflates buttons A and B (from an xml file), and then another LinearLayout that inflates buttons C, D, and E in another xml file.
Buttons A and B (are actually ToggleButtons):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
<ToggleButton
android:id="@+id/A"
android:textOn=""
android:textOff=""
android:background="@layout/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="30dp"
/>
<ToggleButton
android:id="@+id/B"
android:textOn=""
android:textOff=""
android:background="@layout/B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
</LinearLayout>
</RelativeLayout>
Buttons C,D,E xml file:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
<ImageView
android:id="@+id/C"
android:src="@drawable/C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
<ImageView
android:id="@+id/D"
android:src="@drawable/D"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
<ImageView
android:id="@+id/E"
android:src="@drawable/E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
</LinearLayout>
</RelativeLayout>
My code basically works, but I have to fudge with the margins to make things line up correctly (which they don’t yet). I wonder if there’s some cleaner way of center aligning button-sets “A B” and “C D E”
ps: the astute reader will notice that I’m extending LinearLayout, but inflating RelativeLayouts. (I don’t know why it can even work at all, but) when I tried extending RelativeLayout instead, the “C D E” layout didn’t even appear on my device. I don’t know where it went.
Use a linear layout as the main body.
Then use
layout_gravityon the individual horizontal linearlayouts to keep the content centredUse the
layout_marginon each of the child view to space them apart. I have specified this as15dipbut you should use a dimension so you can modify them all together.