Adam N
Adam N

Reputation: 97

Full screen android layout with rows having the same height

I try to do android layout like this:

+---------+
|   20%   |
+---------+
|   20%   |
+---------+
|   20%   |
+---------+
|   20%   |
+---------+
|   20%   |
+---------+

But I can't do that. I tried each code i found in Internet. It's my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
        android:weightSum="5" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#33B5E5"
        android:orientation="vertical" >
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#93B5E5"
        android:orientation="vertical" >
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#33B5E5"
        android:orientation="vertical" >
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#93B5E5"
        android:orientation="vertical" >
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#33B5E5"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Views: 223

Answers (2)

josedlujan
josedlujan

Reputation: 5600

Use this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"

        android:weightSum="5" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#33B5E5"
        android:orientation="vertical" >
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#93B5E5"
        android:orientation="vertical" >
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#33B5E5"
        android:orientation="vertical" >
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#93B5E5"
        android:orientation="vertical" >
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:background="#33B5E5"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

Upvotes: 1

telkins
telkins

Reputation: 10550

You had everything mostly correct by setting the height of each view to 0dp and then setting the layout_weight to 1. What you forgot is that layout_weight is only used for LinearLayout, so you needed to change your RelativeLayout for it to work. Also note that weight_sum doesn't do much in this case since your total weight already equals 5.

Upvotes: 0

Related Questions