Android layout-weight:How to explain the calculation of proportion when width set 'match_parent'

Viewed 1422

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

1 Answers
Related