android:layout_weight是什么意思?

我不明白如何使用这个属性。 任何人都可以告诉我更多关于它吗?

使用layout_weight可以指定多个视图之间的大小比例。 比如你有一个MapView和一个table ,它应该给地图显示一些额外的信息。 地图应该使用3/4的屏幕,桌子应该使用1/4的屏幕。 然后,将maplayout_weight设置为3,将tablelayout_weight设置为1。

为了得到它的工作,你也必须设置高度或宽度(取决于你的方向)为0px。

简而言之, layout_weight指定布局中多余的空间分配给View。

LinearLayout支持为每个孩子分配一个权重。 此属性为视图分配“重要性”值,并允许其展开以填充父视图中的任何剩余空间。 视图的默认权重为零。

计算分配任何剩余的孩子之间的空间

一般来说,公式是:

分配给孩子的空间=(孩子的个人体重)/(线性布局中每个孩子的体重总和)

例1

如果有三个文本框,其中两个声明权重为1,而第三个没有权重(0),则剩余空间分配如下:

第一个文本框= 1 /(1 + 1 + 0)

第二个文本框= 1 /(1 + 1 + 0)

第三个文本框= 0 /(1 + 1 + 0)

例2

假设我们有一个水平行的文本标签和两个文本编辑元素。 该标签没有指定layout_weight ,所以它占用了最小的渲染空间。 如果两个文本编辑元素的layout_weight设置为1,则父级布局中的剩余宽度将在它们之间平均分配(因为我们声称它们同样重要)。

计算:

第一个标签= 0 /(0 + 1 + 1)

第二个文本框= 1 /(0 + 1 + 1)

第三个文本框= 1 /(0 + 1 + 1)

如果第一个文本框的layout_weight为1,而第二个文本框的layout_weight为2,那么剩余空间的三分之一将被赋予第一个,三分之二到第二个(因为我们声称第二个更重要)。

计算:

第一个标签= 0 /(0 + 1 + 2)

第二个文本框= 1 /(0 + 1 + 2)

第三个文本框= 2 /(0 + 1 + 2)


源文章

添加到其他答案,让这个工作最重要的是将布局的宽度(或高度)设置为0px

 android:layout_width="0px" 

否则你会看到垃圾

如果有多个视图跨越一个LinearLayout ,那么layout_weight给它们每个比例的大小。 一个更大的layout_weight值的视图“重”更多,所以它获得更大的空间。

这是一个图像,使事情更清晰。

在这里输入图像描述

理论

术语布局权重与math中加权平均的概念有关。 就像在一个大学课上,作业价值30%,出勤率10%,期中值20%,最终值40%。 这些部分的分数加在一起后会给出总分数。

在这里输入图像描述

布局重量也是一样的。 水平LinearLayoutViews可以占据总宽度的一定百分比。 (或垂直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,但您可以使用任何数字。 这只是总的问题。 你可以看到我张贴的第一张图片中的三排button,数字都是不同的,但由于比例相同,加权宽度在每一行中都不会改变。 有些人喜欢使用总和为1的十进制数字,以便在复杂的布局中清楚每个部分的重量是多less。

最后一个音符。 如果您有大量使用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> 

layout_weight告诉Android如何在LinearLayout分发View 。 然后,Android首先计算所有具有指定权重的View所需的总比例,并根据它指定的屏幕的哪一部分来放置每个View 。 在下面的例子中,Android看到TextViewlayout_weight0 (这是默认的), EditTextlayout_weight2 ,而Button的权重是1 。 所以Android分配“足够”的空间来显示tvUsernametvPassword ,然后将其余的屏幕宽度分成5个相等的部分,其中两个分配给etUsername ,两个分配给etUsername ,最后一个分配给bLogin

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

看起来像:
风景取向
肖像取向

这样想,会更简单

如果你有3个button,它们的权重是1,3,1,那么它就像HTML中的表一样工作

为该行提供5个部分:1个button1部分,3个button2部分和1个button1部分

看待,

对我来说最好的解释之一就是这个(从Android教程中,find第7步) :

在LinearLayouts中使用layout_weight为布局中的视图分配“重要性”。 所有视图的默认layout_weight为零,这意味着它们只占用屏幕上的空间,因为它们需要显示。 指定大于零的值将根据每个视图的layout_weight的值以及其与此视图元素和其他视图元素的当前布局中指定的总体layout_weight的比率,拆分父视图中剩余的可用空间。

举个例子:比方说,我们有一个文本标签和两个文本编辑元素在水平行。 该标签没有指定layout_weight,所以它占用了最小的渲染空间。 如果两个文本编辑元素中的每一个的layout_weight设置为1,则父级布局中的剩余宽度将在它们之间平均分配(因为我们声称它们同样重要)。 如果第一个layout_weight为1,第二个layout_weight为2,那么剩余空间的三分之一将被赋予第一个,而第二个的三分之二到第二个(因为我们声称第二个更重要)。

http://developer.android.com/guide/topics/ui/layout-objects.html#linearlayout

layout_weight定义控件必须分别获得多less空间到其他控件。

结合两者的答案

Flo&rptwsthi和roetzi,

不要忘记改变你的layout_width=0dp/px ,否则layout_weight行为将会以最大的数量占据最小的空间和最小的数量占据最大的空间。

此外,一些重量组合会导致一些布局不能显示(因为它占据了空间)。

小心这个。

请看LinearLayout的weightSum和每个View的layout_weight。 Android:weightSum =“4”android:layout_weight =“2”android:layout_weight =“2”他们的layout_height都是0px,但我不确定它是相关的

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="4"> <fragment android:name="com.example.SettingFragment" android:id="@+id/settingFragment" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="2" /> <Button android:id="@+id/dummy_button" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="2" android:text="DUMMY" /> </LinearLayout> 

顾名思义,布局权重指定特定字段或小部件占用屏幕空间的空间量或百分比。
如果我们在水平方向上指定重量,那么我们必须指定layout_width = 0px
同样,如果我们指定垂直方向的重量,那么我们必须指定layout_height = 0px