我不明白如何使用这个属性。 谁能告诉我更多关于它的事情? android
添加到其余答案,使这个工做最重要的是将布局宽度(或高度)设置为0px 布局
android:layout_width="0px"
不然你会看到垃圾 性能
结合两个答案 spa
Flo&rptwsthi和roetzi, code
请记住更改layout_width=0dp/px
,不然layout_weight
行为将反向行动,最大数量占用最小空间,最小数量占用最大空间。 xml
此外,一些权重组合会致使一些布局没法显示(由于它占据了空间)。 utf-8
要当心这一点。 get
顾名思义,布局权重指定特定字段或窗口小部件占据屏幕空间的空间量或百分比。
若是咱们在水平方向指定权重,那么咱们必须指定layout_width = 0px
。
一样,若是咱们在垂直方向指定权重,那么咱们必须指定layout_height = 0px
。 数学
layout_weight
告诉Android如何在LinearLayout
分发您的View
。 Android而后首先计算全部具备指定权重的View
所需的总比例,并根据所需的屏幕分数放置每一个View
。 在下面的示例中,Android看到TextView
的layout_weight
为0
(这是默认值), EditText
的layout_weight
为2
,而Button
的权重为1
。 所以Android分配“刚够”的空间来显示tvUsername
和tvPassword
而后将所述屏幕宽度的剩余部分分红5个等份,其中的两个被分配给etUsername
,两到etPassword
和最后部分bLogin
: it
<LinearLayout android:orientation="horizontal" ...> <TextView android:id="@+id/tvUsername" android:text="Username" android:layout_width="wrap_content" ... /> <EditText android:id="@+id/etUsername" android:layout_width="0dp" android:layout_weight="2" ... /> <TextView android:id="@+id/tvPassword" android:text="Password" android:layout_width="wrap_content" /> <EditText android:id="@+id/etPassword" android:layout_width="0dp" android:layout_weight="2" ... /> <Button android:id="@+id/bLogin" android:layout_width="0dp" android:layout_weight="1" android:text="Login"... /> </LinearLayout>
看起来像: 和
若是有多个视图跨越LinearLayout
,则layout_weight
会为每一个视图提供一个比例大小。 具备更大layout_weight
值的视图“更重”,所以它得到更大的空间。
这是一个让事情更清晰的图像。
术语布局权重与数学中加权平均的概念有关。 就像在大学课堂上,做业价值30%,出勤率10%,期中价值20%,决赛价值40%。 这些部分的分数在加权后会给出总分。
布局重量也是同样的。 水平LinearLayout
的Views
能够占据总宽度的必定百分比。 (或垂直LinearLayout
的高度的百分比。)
您使用的LinearLayout
将以下所示:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- list of subviews --> </LinearLayout>
请注意,必须对LinearLayout
使用layout_width="match_parent"
。 若是你使用wrap_content
,那么它将没法工做。 另请注意, layout_weight
不适用于RelativeLayouts中的视图(有关此问题的SO答案,请参阅此处和此处 )。
水平LinearLayout
每一个视图都以下所示:
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" />
请注意,您须要将layout_width="0dp"
与layout_weight="1"
一块儿使用。 忘记这会致使许多新用户出现问题。 (请参阅此文章 ,了解经过不将宽度设置为0可得到的不一样结果。)若是您的视图位于垂直 LinearLayout
那么您固然会使用layout_height="0dp"
。
在上面的Button
示例中,我将权重设置为1,但您可使用任何数字。 重要的只是总数。 您能够在我发布的第一张图像中的三行按钮中看到,数字都不一样,但因为比例相同,所以每行的加权宽度不会改变。 有些人喜欢使用总和为1的十进制数,这样在复杂的布局中,每一个部分的重量都很清楚。
最后一点说明。 若是您有许多使用layout_weight
的嵌套布局,则可能对性能不利。
如下是顶部图像的xml布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android:layout_weight=" android:textSize="24sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="2" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android:layout_weight=" android:textSize="24sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="10" android:text="10" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:text="20" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="10" android:text="10" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android:layout_weight=" android:textSize="24sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".25" android:text=".25" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".50" android:text=".50" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".25" android:text=".25" /> </LinearLayout> </LinearLayout>