I'm new to android.When learning about the 'weight' property of layout,I got confused.
I know that when the layout_width
is set to 0dp,each element will occupy weight/weightSum
.
But when the layout_width
is set to match_parrent(perhaps not recommended though),it's a little complicated.
Somebody says the formula is:
delta = 1-numOfElement;
proportion[ i ] = 1+delta*(weight[ i ]/weightSum);
Let me give an example to make it clear~
<?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="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/edit_message"
android:layout_width="match_parent"
android:layout_weight="2"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:text="@string/button_cancel"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="sendMessage"/>
<Button
android:text="@string/button_send"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
There are 3 elements,so delta=1-3=-2;
weightSum=(2+3+4)=9;
proportion[ 0 ] = 1+(-2)(2/9)=5/9;
proportion[ 1 ] = 1+(-2)(3/9)=3/9;
proportion[ 2 ] = 1+(-2)*(4/9)=1/9;
So it's actually 5:3:1
But I don't understand the meaning,could anybody explain that?~
Or if the formula is wrong,please correct it~Thanks
If there are 2 views in the LinearLayout, the first with a layout_weight of 1, the second with a layout_weight of 2 and no weightSum is specified, by default, the weightSum is calculated to be 3 (sum of the weights of the children) and the first view takes 1/3 of the space while the second takes 2/3.
However, if we were to specify the weightSum as 5, the first would take 1/5th of the space while the second would take 2/5th. So a total of 3/5th of the space would be occupied by the layout keeping the rest empty.
Calculation to assign any Remaining/Extra space between child. (not the total space)
space assign to child = (child individual weight) / (sum of weight of every child in Linear Layout)
if any one of the children is given 0 weight then it takes up the minimum space required to render and rest of available space is distributed among the children by the above formula
The Reverse expansion takes place in your case since: because you are using match_parent as the layout_width. The weight is used to distribute the remaining empty space,and your first element has match parent therefore it tries to occupy the highest space horizontally and remaining lesser space is distributed among two other children.
Again for the second child,it tries to expand to the maximum space available leaving a very less space for the last child,therefore expansion is totally reverse to the applied individual weights.
In other words, match_parent dominates like a boss in this case. No specific calculation appears here rather Priority to the children in order can be seen very clearly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With