android 中的 android:weightSum 是什么,它是如何工作的?
我想知道:什么是 android:weightSum 和布局权重,它们如何工作?
I want to know: What is android:weightSum and layout weight, and how do they work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
补充superM和Jeff的答案,
如果LinearLayout中有2个视图,第一个的layout_weight为1,第二个的layout_weight为2并且没有指定weightSum,默认情况下,weightSum计算为3(总和孩子们的体重),第一个视图占空间的 1/3,而第二个视图占空间的 2/3。
但是,如果我们将weightSum 指定为5,则第一个将占用空间的1/5,而第二个将占用空间的2/5。因此,总共 3/5 的空间将被布局占据,而其余空间则为空。
Adding on to superM's and Jeff's answer,
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.
根据文档,
android:weightSum
定义最大权重总和,如果未明确指定,则计算为所有子级的layout_weight
之和。让我们考虑一个带有水平方向的 LinearLayout 和其中的 3 个 ImageViews 的示例。现在我们希望这些 ImageViews 始终占据相同的空间。要实现此目的,您可以将每个
ImageView
的layout_weight
设置为 1,并且weightSum
将计算为等于 3,如下所示评论。weightSum
对于为任何设备正确呈现布局非常有用,如果直接设置宽度和高度,则不会发生这种情况。Per documentation,
android:weightSum
defines the maximum weight sum, and is calculated as the sum of thelayout_weight
of all the children if not specified explicitly.Let's consider an example with a
LinearLayout
with horizontal orientation and 3ImageViews
inside it. Now we want theseImageViews
always to take equal space. To acheive this, you can set thelayout_weight
of eachImageView
to 1 and theweightSum
will be calculated to be equal to 3 as shown in the comment.weightSum
is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.权重总和完全按照您想要的方式工作(与其他答案一样,您不必将父布局上的所有权重相加)。在子视图上指定您希望其承受的重量。 不要忘记指定
以下是一个示例
,它看起来像
Weight sum works exactly as you want (like other answers you don't have to sum all the weights on parent layout). On child view specify the weight you want it to take. Don't forget to specify
Following is an example
This will look like
文档说得最好,并包含一个示例(突出显示我的)。
因此,为了更正 superM 的示例,假设您有一个水平方向的 LinearLayout,其中包含两个 ImageView 和一个 TextView。您将
TextView
定义为固定大小,并且希望两个ImageView
平均占用剩余空间。为了实现这一点,您可以将
layout_weight
1 应用于每个ImageView
,对TextView
不应用任何内容,并且weightSum
为2.0 在LinearLayout
上。The documentation says it best and includes an example, (highlighting mine).
So to correct superM's example, suppose you have a
LinearLayout
with horizontal orientation that contains twoImageViews
and aTextView
with. You define theTextView
to have a fixed size, and you'd like the twoImageViews
to take up the remaining space equally.To accomplish this, you would apply
layout_weight
1 to eachImageView
, none on theTextView
, and aweightSum
of 2.0 on theLinearLayout
.经过一番实验,我认为 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。最小扩展上限为0。最大扩展上限为父级大小,即权重上限为0。
如果某个元素设置为
wrap_content
,则先计算其扩展,剩余扩展为服从fill_parent
元素之间的分配。如果设置了weightSum
,则会导致layout_weight
对wrap_content
元素没有影响。但是,
wrap_content
元素仍然可以被权重低于 (例如,对于weightSum
= 1,为 0-1 之间;对于上述示例,为 0-20 之间)。如果未指定
weightSum
,则将其计算为所有layout_weight
值的总和,包括设置了wrap_content
的元素!因此,在wrap_content
元素上设置layout_weight
,可以影响它们的扩展。例如,负权重会缩小其他fill_parent
元素。在布局
fill_parent
元素之前,是否将上述公式应用于wrap_content
元素,最大可能的扩展是它们根据包装内容的扩展。wrap_content
元素将被收缩,然后计算并分配剩余fill_parent
元素的最大可能扩展。这可能会导致不直观的结果。
After some experimenting, I think the algorithm for LinearLayout is this:
Assume that
weightSum
is set to a value. The case of absence is discussed later.First, divide the
weightSum
by the number of elements whithmatch_parent
orfill_parent
in the dimension of the LinearLayout (e.g.layout_width
fororientation="horizontal"
). We will call this value the weight multiplier for each element. The default value forweightSum
is 1.0, so the default weight multiplier is1/n
, wheren
is the number offill_parent
elements;wrap_content
elements do not contribute ton
.E.g. when
weightSum
is 60, and there are 3fill_parent
elements, the weight multiplier is 20. The weight multiplier is the default value for e.g.layout_width
if the attribute is absent.Second, the maximum possible expansion of every element is computed. First, the
wrap_content
elements are computed according to their contents. Their expansion is deducted from the expansion of the parent container. We will call the remainerexpansion_remainer
. This remainder is distributed amongfill_parent
elements according to theirlayout_weight
.Third, the expansion of every
fill_parent
element is computed as:Example:
If
weightSum
is 60, and there are 3fill_parent
elements with the weigths 10, 20 and 30, their expansion on the screen is 2/3, 1/3 and 0/3 of the parent container.The minimum expansion is capped at 0. The maximum expansion is capped at parent size, i.e. weights are capped at 0.
If an element is set to
wrap_content
, its expansion is calculated first, and the remaining expansion is subject to distribution among thefill_parent
elements. IfweightSum
is set, this leads tolayout_weight
having no effect onwrap_content
elements.However,
wrap_content
elements can still be pushed out of the visible area by elements whose weight is lower than (e.g. between 0-1 forweightSum
= 1 or between 0-20 for the above example).If no
weightSum
is specified, it is computed as the sum of alllayout_weight
values, including elements withwrap_content
set! So havinglayout_weight
set onwrap_content
elements, can influence their expansion. E.g. a negative weight will shrink the otherfill_parent
elements.Before the
fill_parent
elements are laid out, will the above formula be applied towrap_content
elements, with maximum possible expansion being their expansion according to the wrapped content. Thewrap_content
elements will be shrunk, and afterwards the maximum possible expansion for the remainingfill_parent
elements is computed and distributed.This can lead to unintuitive results.
如果未指定,则通过添加所有子项的布局权重来计算总和。例如,可以通过将layout_weight设置为0.5并将weightSum设置为1.0来为单个子项提供总可用空间的50%。必须是浮点值,例如“1.2”
If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0. Must be a floating point value, such as "1.2"
一件似乎没有人提到的事情:假设您有一个 vertical
LinearLayout
,因此为了使其内部的布局/元素/视图中的权重能够 100% 工作正确 - 所有这些都必须将layout_height
属性(必须存在于您的 xml 文件中)设置为0dp
。在某些情况下,似乎任何其他值都会把事情搞砸。One thing which seems like no one else mentioned: let's say you have a vertical
LinearLayout
, so in order for the weights in layout/element/view inside it to work 100% properly - all of them must havelayout_height
property (which must exist in your xml file) set to0dp
. Seems like any other value would mess things up in some cases.布局权重的工作原理类似于比率。例如,如果有一个垂直布局,并且有两个项目(例如按钮或文本视图),其中一个的布局权重为 2,另一个的布局权重为 3。那么第一项将占据屏幕/布局的 5 部分中的 2 部分,另一个项目将占据 5 部分中的 3 部分。这里5是权重总和。即权重总和将整个布局划分为定义的部分。
布局权重定义特定项目在预定义的总权重总和中占据多少部分。权重总和也可以手动声明。当使用线性布局进行 UI 设计时,按钮、文本视图、编辑文本等都使用权重和布局权重进行组织。
Layout Weight works like a ratio. For example, if there is a vertical layout and there are two items(such as buttons or textviews), one having layout weight 2 and the other having layout weight 3 respectively. Then the 1st item will occupy 2 out of 5 portion of the screen/layout and the other one 3 out of 5 portion. Here 5 is the weight sum. i.e. Weight sum divides the whole layout into defined portions.
And Layout Weight defines how much portion does the particular item occupies out of the total Weight Sum pre-defined. Weight sum can be manually declared as well. Buttons, textviews, edittexts etc all are organized using weightsum and layout weight when using linear layouts for UI design.
来自开发人员 文档
例如,这可以用来给出通过将layout_weight 设置为
0.5
并将weightSum 设置为1.0
,将单个子项占总可用空间的50%
。除了 @Shubhayu 答案之外,
其余
3/5
可用于其他子布局,这些子布局实际上不需要包含布局的任何特定部分。这是 android:weightSum 属性的潜在用途。
From developer documentation
This can be used for instance to give a single child
50%
of the total available space by giving it a layout_weight of0.5
and setting the weightSum to1.0
.Addition to @Shubhayu answer
rest
3/5
can be used for other child layouts which really doesn't need any specific portion of containing layout.this is potential use of
android:weightSum
property.没有人明确提到
weightSum
是LinearLayout
的一种特定 XML 属性。我相信这对于像我一样一开始感到困惑、在
ConstraintLayout
文档中查找weightSum
的人会有帮助。No one has explicitly mentioned that
weightSum
is a particular XML attribute forLinearLayout
.I believe this would be helpful to anyone who was confused at first as I was, looking for
weightSum
in theConstraintLayout
documentation.