LinearLayout with LayoutWeight not working

899 views Asked by At

I have the following layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/color_brand"
                android:weightSum="100">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:background="@color/color_white">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:background="@color/color_black"
        android:layout_below="@id/top">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:background="@color/color_white"
        android:layout_below="@id/middle">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>

    </LinearLayout>

</RelativeLayout>

I want a 40-20-40 split between the layouts, and I've tried everything, but nothing seems to work. I've tried adding an empty view in the linear layouts, I've given the views in the linear layout the weight, but nothing is working. Can someone point out what I'm doing wrong?

9

There are 9 answers

0
Rahul On BEST ANSWER

Change your main root parent to LinearLayout and give it a vertical orientation. RelativeLayout don't support weightsum, as you see in your code you are defining 0dp for height so you have to make your root view LinearLayout with vertical orientation to make weightage work.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/color_brand"
            android:weightSum="100">

     --------
</LinearLayout>
0
Bhargav Ghodasara On

You must have to take LinearLayout as parent for use weightSum because,RelativeLayout don't support weightSum. Now you have to take LinearLayout instead of RelativeLayout. You have to write your CODE like below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/color_brand"
 android:orientation="vertical"
 android:weightSum="100">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:background="@color/color_white">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/top"
        android:layout_weight="20"
        android:background="@color/color_black">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/middle"
        android:layout_weight="40"
        android:background="@color/color_white">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>


</LinearLayout>
0
teck wei On

Try this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/color_brand">

<LinearLayout
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="40"
    android:background="@color/color_white"
   >

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        />

</LinearLayout>

<LinearLayout
    android:id="@+id/middle"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="20"
    android:background="@color/color_black"
    android:layout_below="@id/top"

    >

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
       />

</LinearLayout>

<LinearLayout
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="40"
    android:background="@color/color_white"
    android:layout_below="@id/middle"

   >

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        />

</LinearLayout>

Your parent is Relative layout that why doesn't work

0
Harshad Pansuriya On

WeightSum only work with the LinearLayout. So you have to change your parent RelativeLayout to LinearLayout.

So Change your this code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/color_brand"
                android:weightSum="100">

to this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/color_brand"
                android:weightSum="100"
                android:orientation="vertical">

Note : add orientation in the LinearLayout.

0
Kaushik On

android:weightSum is not an attribute of RelativeLayout it is an attribute of LinearLayout. So you can change the parent layout to LinearLayout or you can use PercentRelativeLayout

code snippet

<android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentRelativeLayout>
0
Amit Soni On

remove your Relative Layout OR change it to Linear with your orientation. It will work.

0
Rishabh Mahatha On
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:background="@color/colorBlack"
        android:layout_below="@id/top">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:layout_below="@id/middle">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>

    </LinearLayout>

</LinearLayout>

Use this will solve your issue. And one more thing when your want to manage your layout according to weight then you have to use LINEAR LAYOUT because the weight concept is not working in RELATIVE LAYOUT.

0
Govinda P On

Try This For 20-40-20

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="40"
        android:background="@android:color/darker_gray">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="20"
        android:background="@android:color/black"
        android:layout_below="@id/top">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="40"
        android:background="@android:color/darker_gray"
        android:layout_below="@id/middle">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>
</LinearLayout>  

Output:
enter image description here

Try this for 40-20-40

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="10">
    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="3"
        android:background="@android:color/black"
        android:layout_below="@id/top">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="4"
        android:background="@android:color/darker_gray">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>


    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="3"
        android:background="@android:color/black"
        android:layout_below="@id/middle">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>

</LinearLayout>  

Output
enter image description here

1
B Patel On

I thin you have to Replace Relativelayout with Linearlayout.