Reputation: 23483
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?
Upvotes: 3
Views: 918
Reputation: 378
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>
Upvotes: 1
Reputation: 20910
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 theLinearLayout
.
Upvotes: 1
Reputation: 3319
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>
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>
Upvotes: 0
Reputation: 1271
<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.
Upvotes: 1
Reputation: 39
remove your Relative Layout OR change it to Linear with your orientation. It will work.
Upvotes: 1
Reputation: 6162
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>
Upvotes: 1
Reputation: 10625
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>
Upvotes: 3
Reputation: 1385
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
Upvotes: 1