我是android新手。在了解布局的“weight”属性时,我感到困惑。
我知道当layout_width
设置为0dp时,每个元素将占用weight/weightSum
。 但是当layout_width
设置为match_parrent(可能不推荐),但它有点复杂。
有人说,公式为:
Android布局权重:如何解释宽度设置'match_parent'时的比例计算
,δ= 1-numOfElement;
ratio [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个元素,因此,δ= 1-3 = -2;
weightSum =(2 + 3 + 4)= 9;
ratio [0] = 1 +( - 2)(2/9)= 5/9;
比例[1] = 1 +( - 2)(3/9)= 3/9;
比例[2] = 1 +( - 2)*(4/9)= 1/9;
所以它实际上是5:3:1
但我不明白什么意思,可能有人解释说〜
或者,如果公式有误,请改正〜谢谢
我改变了我对你的具体问题的回答@disinuo –