【发布时间】:2011-11-19 03:29:47
【问题描述】:
我想知道:什么是android:weightSum和布局权重,它们是如何工作的?
【问题讨论】:
标签: android android-layout android-layout-weight
我想知道:什么是android:weightSum和布局权重,它们是如何工作的?
【问题讨论】:
标签: android android-layout android-layout-weight
加上 superM 和 Jeff 的回答,
如果LinearLayout中有2个view,第一个layout_weight为1,第二个layout_weight为2且未指定weightSum,默认计算weightSum为3(权重之和children),第一个视图占空间的 1/3,而第二个视图占 2/3。
但是,如果我们将 weightSum 指定为 5,则第一个将占用 1/5 的空间,而第二个将占用 2/5 的空间。因此总共 3/5 的空间将被布局占用,其余部分为空。
【讨论】:
根据文档,android:weightSum 定义了最大权重总和,如果未明确指定,则计算为所有子代的 layout_weight 的总和。
让我们考虑一个示例,其中有一个水平方向的LinearLayout,其中有3 个ImageViews。现在我们希望这些ImageViews 始终占据相等的空间。为了实现这一点,您可以将每个ImageView 的layout_weight 设置为1,并且weightSum 将被计算为等于3,如评论中所示。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- android:weightSum="3" -->
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"/>
.....
weightSum 有助于为任何设备正确呈现布局,如果您直接设置宽度和高度,则不会发生这种情况。
【讨论】:
weightSum目的的解释。您的示例中的行为将与省略 weightSum 相同,实际上在这种情况下不应指定 weightSum。 documentation 说,weightSum Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children.
权重总和完全按照您的意愿工作(与其他答案一样,您不必对父布局上的所有权重求和)。在子视图上指定您希望它承受的重量。 别忘了指明
android:layout_width="0dp"
以下是一个例子
<LinearLayout
android:layout_width="500dp"
android:layout_height="20dp" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="@android:color/holo_green_light"
android:gravity="center"
android:text="30%"
android:textColor="@android:color/white" >
</TextView>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@android:color/holo_blue_bright"
android:gravity="center"
android:text="20%"
android:textColor="@android:color/white" >
</TextView>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:background="@android:color/holo_orange_dark"
android:gravity="center"
android:text="50%"
android:textColor="@android:color/white" >
</TextView>
</LinearLayout>
这看起来像
【讨论】:
documentation 说得最好,包括一个例子,(突出显示我的)。
android:weightSum
定义最大重量总和。如果未指定,则总和由下式计算 添加所有孩子的 layout_weight。这可以用于 实例为 single 孩子提供总可用空间的 50% 给它一个 0.5 的 layout_weight 并将 weightSum 设置为 1.0。
为了纠正 superM 的示例,假设您有一个水平方向的LinearLayout,其中包含两个ImageViews 和一个TextView。您将TextView 定义为具有固定大小,并且您希望两个ImageViews 平均占用剩余空间。
要完成此操作,您需要对每个 ImageView 应用 layout_weight 1,在 TextView 上不应用,在 LinearLayout 上应用 2.0 的 weightSum。
【讨论】:
经过一些实验,我认为LinearLayout的算法是这样的:
假设weightSum 设置为一个值。缺席的情况在后面讨论。
首先,将weightSum 除以LinearLayout 维度中match_parent 或fill_parent 的元素数(例如layout_width 对应orientation="horizontal")。我们将此值称为每个元素的权重乘数 。 weightSum 的默认值为 1.0,因此默认权重乘数为1/n,其中n 是fill_parent 元素的数量; wrap_content 元素对 n 没有贡献。
例如当weightSum 为 60,并且有 3 个fill_parent 元素时,权重乘数为 20。权重乘数是例如的默认值。 layout_width 如果该属性不存在。
其次,计算每个元素的最大可能扩展。首先,wrap_content 元素是根据它们的内容计算的。它们的扩展是从父容器的扩展中扣除的。我们将调用其余的expansion_remainer。这个剩余部分根据layout_weight 分配给fill_parent 元素。
第三,每个fill_parent元素的扩展计算为:
例子:
如果weightSum是60,并且有3个fill_parent元素的权重分别为10、20和30,它们在屏幕上的扩展是父容器的2/3、1/3和0/3。
weight | expansion
0 | 3/3
10 | 2/3
20 | 1/3
30 | 0/3
40 | 0/3
最小扩展上限为 0。最大扩展上限为父大小,即权重上限为 0。
如果一个元素设置为wrap_content,首先计算它的扩展,剩余的扩展受fill_parent元素之间的分配。如果设置了weightSum,这将导致layout_weight 对wrap_content 元素没有影响。
但是,wrap_content 元素仍然可以被权重低于 的元素推出可见区域(例如,在 0-1 之间,weightSum= 1 或在 0-20 之间,对于上面的示例)。
如果未指定weightSum,则计算为所有layout_weight 值的总和,包括 设置了wrap_content 的元素!所以在wrap_content 元素上设置layout_weight,可以影响它们的扩展。例如。负权重会缩小其他fill_parent 元素。
在布置fill_parent 元素之前,是否将上述公式应用于wrap_content 元素,最大可能的扩展是它们根据包装内容的扩展。 wrap_content 元素将被缩小,然后计算和分配剩余 fill_parent 元素的最大可能扩展。
这可能会导致不直观的结果。
【讨论】:
如果未指定,则通过添加所有子项的 layout_weight 来计算总和。例如,这可以用于通过将 layout_weight 设置为 0.5 并将 weightSum 设置为 1.0 来为单个子项提供总可用空间的 50%。必须是浮点值,如“1.2”
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_rel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2.0" >
<RelativeLayout
android:id="@+id/child_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#0000FF" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/child_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#00FF00" >
</RelativeLayout>
</LinearLayout>
【讨论】:
似乎没有其他人提到过的一件事:假设您有一个 垂直 LinearLayout,因此为了使其内部布局/元素/视图中的权重能够 100% 正常工作 -它们都必须将 layout_height 属性(必须存在于您的 xml 文件中)设置为 0dp。在某些情况下,似乎任何其他值都会把事情搞砸。
【讨论】:
布局权重就像一个比率。例如,如果有一个垂直布局并且有两个项目(例如按钮或文本视图),一个具有布局权重 2,另一个具有布局权重3分别。然后第一项将占据屏幕/布局的 5 部分中的 2 部分,而另一部分将占据 5 部分中的 3 部分。这里5是重量总和。即 权重总和将整个布局划分为定义的部分。 并且布局重量定义了特定项目在预定义的总重量总和中所占的比例。重量总和也可以手动声明。在 UI 设计中使用线性布局时,按钮、文本视图、编辑文本等都使用 weightsum 和布局权重进行组织。
【讨论】:
来自开发者documentation
这可以用于例如通过将 layout_weight 设置为 0.5 并将 weightSum 设置为 1.0 来将总可用空间的单个子 50% 分配给它。
添加到@Shubhayu 答案
rest 3/5 可用于其他真正不需要包含布局的任何特定部分的子布局。
这是android:weightSum 属性的潜在用途。
【讨论】:
没有人明确提到weightSum 是LinearLayout 的特定XML 属性。
我相信这对像我一样一开始感到困惑的人会有所帮助,在 ConstraintLayout 文档中寻找 weightSum。
【讨论】: