Reputation: 1587
I have three horizontal layouts inside a vertical layout. I want these three horizontal child layouts to occupy the same space so I am adding weights (0.33) to each.
I am assigning the layout heights of the three child layout components to 0dp because the parent layout is vertical but the child layouts are not occupying equal height as expected.
<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"
android:background="@drawable/paper"
android:paddingLeft="4dp"
android:paddingRight="8dp"
tools:context=".TestActivity" >
<LinearLayout
android:id="@+id/scratchLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/topLayout"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.33"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/topArea1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:paddingRight="3dp"
android:text="Top Area 2"
android:textSize="15dp" />
<TextView
android:id="@+id/topArea2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:paddingRight="3dp"
android:text="Top Area 1"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/centerLayout"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.33"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/centerArea1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:text="Center Area 1" />
<TextView
android:id="@+id/centerArea2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:text="Center Area 2" />
</LinearLayout>
<LinearLayout
android:id="@+id/BottomLayout"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.33"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/bottomArea1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:text="Bottom Area 1" />
<TextView
android:id="@+id/bottomArea2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:text="Bottom Area 2" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Upvotes: 2
Views: 3095
Reputation: 2440
I agree with Kabuko, please change all fill_parent's to match_parent, however.
I recommend changing the height to 0dp as you mentioned but setting weight 1 for all of the LinearLayout's.
The weight value is a "factor" not a percentage.
Please refer to this What does android:layout_weight mean? for a cleared example of how you might use layout_weight
Upvotes: 0
Reputation: 36302
Quick notes: Nested weights leads to bad performance, avoid if possible. Don't mix match_parent
and fill_parent
. Just use match_parent
.
Your main issue is that you have layout_height="wrap_content"
for your outer LinearLayout
, but then you have weights. You're specifying each child layout be 1/3 of the total, but then saying the total should be the sum of its children. Should each child layout be 1dp? 100dp? if you want each to be 1/3 of the whole screen, you should set layout_height="match_parent"
for the outer LinearLayout
.
Upvotes: 3