Android layout size: how to explain the aspect ratio calculation when setting the "match_parent" width

I am new to android. When I found out about the "weight" property of a layout, I got confused.
I know that if the parameter is layout_width

set to 0dp , each item will take weight/weightSum

. But when the parameter is layout_width

set to match_parrent (probably not recommended), it's a little tricky.
Someone says that the formula is:

delta = 1-numOfElement;
share [i] = 1 + delta * (weight [i] / weightSum);

Let me give you an example to clarify ~

<?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;
share [0] = 1 + (- 2) (2/9) = 5/9;
share [1] = 1 + (- 2) (3/9) = 3/9;
proportion [2] = 1 + (- 2) * (4/9) = 1/9;

So it really is 5: 3: 1

But I don't understand the point, can someone explain this? ~
Or, if the formula is wrong, correct it ~ Thanks

+3


source to share


1 answer


If there are 2 views in LinearLayout, the first with layout 1, the second with layout 2 and no weightSum, by default the weightSum value is calculated as 3 (the sum of the children weights), and the first view takes 1/3 of the space and the second takes 2/3.

However, if we were to specify weightSum as 5, the first would take 1/5 place and the second would take 2/5. Thus, a total of 3/5 of the space will be taken up by the layout, while remaining 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 of the children is given a weight of 0, then it takes up the minimum space needed for rendering and the rest of the available space is allocated to the children using the formula above



The reverse extension happens in your case, because: because you are using match_parent as layout_width. The weight is used to distribute the remaining white space, and your first element has a parent column, so it tries to occupy the highest horizontal space, and the remaining smaller space is distributed between the other two children.

Again for the second child, he tries to expand access to the maximum available space, leaving very little room for the last child, so the expansion is completely opposite to the individual weights used.

In other words, match_parent dominates like a boss in this case. There are no specific calculations here. The priority for children in order can be seen very clearly.

+1


source







All Articles