Android布局权重:如何解释宽度设置'match

编程入门 行业动态 更新时间:2024-10-24 13:28:07
Android布局权重:如何解释宽度设置'match_parent'时的比例计算(Android layout-weight:How to explain the calculation of proportion when width set 'match_parent')

我是android的新手。当我了解布局的'权重'属性时,我感到困惑。 我知道当layout_width设置为0dp时 ,每个元素都会占用weight/weightSum 。 但是当layout_width设置为match_parrent时 (虽然可能不推荐),但它有点复杂。 有人说公式是:

delta = 1-numOfElement; 比例[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; weightSum =(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;

所以它实际上是5:3:1

但我不明白其含义,有人可以解释一下吗?〜 或者如果公式错误,请更正〜谢谢

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

最满意答案

如果LinearLayout中有2个视图,第一个视图的layout_weight为1,第二个视图的layout_weight为2且没有指定weightSum,默认情况下,weightSum计算为3(子项的权重之和)和第一个视图占用空间的1/3,而第二个视图占用2/3。

但是,如果我们将weightSum指定为5,则第一个占用空间的1/5,而第二个占用2/5。 因此,总共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)

如果任何一个孩子的体重为0,那么它占用了渲染所需的最小空间,其余的可用空间通过上述公式分配给孩子们

反向扩展在您的情况下发生,因为:因为您使用match_parent作为layout_width。 权重用于分配剩余的空白空间,并且您的第一个元素具有匹配的父元素,因此它尝试水平占据最高空间,剩余的较小空间分布在另外两个子元素中。

同样对于第二个孩子,它试图扩展到可用的最大空间,为最后一个孩子留下非常小的空间,因此扩展完全与所应用的个体权重相反。

换句话说,在这种情况下,match_parent像boss一样占主导地位。 这里没有具体的计算方法,因此可以非常清楚地看到有序儿童的优先顺序。

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.

更多推荐

本文发布于:2023-07-31 19:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347272.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:权重   宽度   布局   Android   match

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!