Android layout-weight:width设置时如何计算比例 'match_parent'

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

刚开始android.When学习布局'weight'属性,一头雾水
我知道当layout_width设置为0dp时,每个元素都会占据weight/weightSum。 但是当layout_width设置为match_parrent时(虽然不推荐),有点复杂。
有人说公式是:

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

我举个例子说清楚~

<?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>

有3个元素,所以delta=1-3=-2;
权重总和=(2+3+4)=9;
比例[ 0 ] = 1+(-2)(2/9)=5/9;
比例[ 1 ] = 1+(-2)
(3/9)=3/9;
比例[ 2 ] = 1+(-2)*(4/9)=1/9;

So it's actually 5:3:1

但是我不明白什么意思,谁能解释一下?~
或者公式有误,请指正~谢谢

如果LinearLayout中有2个视图,第一个layout_weight为1,第二个layout_weight为2且没有指定weightSum,默认计算weightSum为 3(children 的权重总和),第一个视图占 space 的 1/3,而第二个占 2/3.

但是,如果我们将 weightSum 指定为 5,则第一个将占用 space 的 1/5,而第二个将占用 2/5。因此,space 的 3/5 将被布局占用,其余部分为空。

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)

如果 children 中的任何一个的权重为 0,则它占用渲染所需的最小值 space,其余可用的 space 分配给 children 由上式

反向扩展发生在您的案例中,因为: 因为您使用 match_parent 作为 layout_width。权重用于分配剩余的空 space,并且您的第一个元素匹配 parent 因此它尝试在水平方向上占据最高的 space 并且剩余较小的 space 分布在另外两个 children.

对于第二个 child,它会尝试扩展到最大可用 space,为最后一个 child 留下非常少的 space,因此完全扩展与应用的个人权重相反。

换句话说, match_parent 在这种情况下像老板一样统治。 这里没有出现具体的计算,而是按children顺序的优先级可以看的很清楚。