Since you want to use RelativeLayout
, the easiest way is to use vertical and horizontal struts (which are invisible) and use those as anchors for your four RelativeLayout
s children. This approach is definitely more efficient than nested LinearLayout
s with weights. You should probably read this before nesting LinearLayout
s with weights.
A snippet from the above link:
Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.
So for using RelativeLayout
s with struts, your XML could be like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<View
android:id="@+id/horizontalStrut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true" />
<View
android:id="@+id/verticalStrut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/horizontalStrut"
android:layout_toLeftOf="@id/verticalStrut"
android:background="@color/red" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/horizontalStrut"
android:layout_toRightOf="@id/verticalStrut"
android:background="@color/black" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/horizontalStrut"
android:layout_toLeftOf="@id/verticalStrut"
android:background="@color/blue" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/horizontalStrut"
android:layout_toRightOf="@id/verticalStrut"
android:background="@color/green" >
</RelativeLayout>
</RelativeLayout>