Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide layout to 3 parts?

I'm trying to divide my layout to 3 equal rows, trying to use LinearLayout and weights, but it doesn't work, it doesn't make any change:

<LinearLayout
    android:id="@+id/rightLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignBottom="@+id/picture"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/picture"
    android:layout_toRightOf="@+id/aligner"
    android:orientation="vertical"
    android:weightSum="3" >

    <LinearLayout
        android:id="@+id/lay1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00ff00" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lay2"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lay3"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>
like image 331
Gintas_ Avatar asked Mar 18 '14 20:03

Gintas_


2 Answers

This will work

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rightLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/lay1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#ff0000" >

    <TextView
        android:id="@+id/textView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lay2"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:background="#00ff00" >

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lay3"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" 
    android:background="#0000ff">

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

Screenshot

like image 65
Yash Krishnan Avatar answered Sep 27 '22 21:09

Yash Krishnan


At first, I didn't realise, that problem was not there. I had ScrollView and it made these layouts not work with weights. I added android:fillViewport="true" to ScrollView and it got fixed.

like image 24
Gintas_ Avatar answered Sep 27 '22 23:09

Gintas_