android 中的 android:weightSum 是什么,它是如何工作的?

发布于 2024-12-04 22:52:32 字数 47 浏览 2 评论 0原文

我想知道:什么是 android:weightSum 和布局权重,它们如何工作?

I want to know: What is android:weightSum and layout weight, and how do they work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

小姐丶请自重 2024-12-11 22:52:32

补充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.

梦开始←不甜 2024-12-11 22:52:32

根据文档,android:weightSum 定义最大权重总和,如果未明确指定,则计算为所有子级的 layout_weight 之和。

让我们考虑一个带有水平方向的 LinearLayout 和其中的 3 个 ImageViews 的示例。现在我们希望这些 ImageViews 始终占据相同的空间。要实现此目的,您可以将每个 ImageViewlayout_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 对于为任何设备正确呈现布局非常有用,如果直接设置宽度和高度,则不会发生这种情况。

Per documentation, android:weightSum defines the maximum weight sum, and is calculated as the sum of the layout_weight of all the children if not specified explicitly.

Let's consider an example with a LinearLayout with horizontal orientation and 3 ImageViews inside it. Now we want these ImageViews always to take equal space. To acheive this, you can set the layout_weight of each ImageView to 1 and the weightSum will be calculated to be equal to 3 as shown in the comment.

<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 is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.

空名 2024-12-11 22:52:32

权重总和完全按照您想要的方式工作(与其他答案一样,您不必将父布局上的所有权重相加)。在子视图上指定您希望其承受的重量。 不要忘记指定

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>

,它看起来像

在此处输入图像描述

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

android:layout_width="0dp" 

Following is an example

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

This will look like

enter image description here

時窥 2024-12-11 22:52:32

文档说得最好,并包含一个示例(突出显示我的)。

android:权重总和

定义最大权重总和。如果未指定,则总和的计算方式为
添加所有子项的layout_weight。这可以用于
实例为单个子提供总可用空间的 50%
将其layout_weight设置为0.5并将weightSum设置为1.0。

因此,为了更正 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).

android:weightSum

Defines the maximum weight sum. 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.

So to correct superM's example, suppose you have a LinearLayout with horizontal orientation that contains two ImageViews and a TextView with. You define the TextView to have a fixed size, and you'd like the two ImageViews to take up the remaining space equally.

To accomplish this, you would apply layout_weight 1 to each ImageView, none on the TextView, and a weightSum of 2.0 on the LinearLayout.

指尖上得阳光 2024-12-11 22:52:32

经过一番实验,我认为 LinearLayout 的算法是这样的:

假设 weightSum 设置为一个值。缺席的情况稍后讨论。

首先,将 weightSum 除以 LinearLayout 维度中具有 match_parentfill_parent 的元素数量(例如 layout_width 为orientation="horizo​​ntal")。我们将此值称为每个元素的权重乘数 w_mweightSum 的默认值为 1.0,因此默认权重乘数为 1/n,其中 nfill_parent< 的数量/代码> 元素; wrap_content 元素对 n 没有贡献。

w_m = WeightSum / #fill_parent

例如,当 weightSum 为 60 时,有 3 个 fill_parent 元素,权重乘数为 20。如果该属性不存在,则权重乘数是默认值,例如 layout_width

其次,计算每个元素的最大可能扩展。首先,根据内容计算 wrap_content 元素。它们的膨胀是从父容器的膨胀中扣除的。我们将剩余部分称为expansion_remainer。剩余部分根据 layout_weight 分布在 fill_parent 元素之间。

第三,每个 fill_parent 元素的扩展计算如下:

w_m - (layout_weight / w_m ) * Maximum_possible_expansion

示例:

如果 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_weightwrap_content 元素没有影响。
但是,wrap_content 元素仍然可以被权重低于 weight 的元素推出可见区域multiplier(例如,对于 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 whith match_parent or fill_parent in the dimension of the LinearLayout (e.g. layout_width for orientation="horizontal"). We will call this value the weight multiplier w_m for each element. The default value for weightSum is 1.0, so the default weight multiplier is 1/n, where n is the number of fill_parent elements; wrap_content elements do not contribute to n.

w_m = weightSum / #fill_parent

E.g. when weightSum is 60, and there are 3 fill_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 remainer expansion_remainer. This remainder is distributed among fill_parent elements according to their layout_weight.

Third, the expansion of every fill_parent element is computed as:

w_m - ( layout_weight / w_m ) * maximum_possible_expansion

Example:

If weightSum is 60, and there are 3 fill_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.

weight | expansion
     0 | 3/3
    10 | 2/3
    20 | 1/3
    30 | 0/3
    40 | 0/3

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 the fill_parent elements. If weightSum is set, this leads to layout_weight having no effect on wrap_content elements.
However, wrap_content elements can still be pushed out of the visible area by elements whose weight is lower than weight multiplier (e.g. between 0-1 for weightSum= 1 or between 0-20 for the above example).

If no weightSum is specified, it is computed as the sum of all layout_weight values, including elements with wrap_content set! So having layout_weight set on wrap_content elements, can influence their expansion. E.g. a negative weight will shrink the other fill_parent elements.
Before the fill_parent elements are laid out, will the above formula be applied to wrap_content elements, with maximum possible expansion being their expansion according to the wrapped content. The wrap_content elements will be shrunk, and afterwards the maximum possible expansion for the remaining fill_parent elements is computed and distributed.

This can lead to unintuitive results.

合约呢 2024-12-11 22:52:32

如果未指定,则通过添加所有子项的布局权重来计算总和。例如,可以通过将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>

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"

    <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>
一直在等你来 2024-12-11 22:52:32

一件似乎没有人提到的事情:假设您有一个 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 have layout_height property (which must exist in your xml file) set to 0dp. Seems like any other value would mess things up in some cases.

留蓝 2024-12-11 22:52:32

布局权重的工作原理类似于比率。例如,如果有一个垂直布局,并且有两个项目(例如按钮或文本视图),其中一个的布局权重为 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.

平生欢 2024-12-11 22:52:32

来自开发人员 文档

例如,这可以用来给出通过将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 of 0.5 and setting the weightSum to 1.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.

落叶缤纷 2024-12-11 22:52:32

没有人明确提到 weightSumLinearLayout 的一种特定 XML 属性。

我相信这对于像我一样一开始感到困惑、在 ConstraintLayout 文档中查找 weightSum 的人会有帮助。

No one has explicitly mentioned that weightSum is a particular XML attribute for LinearLayout.

I believe this would be helpful to anyone who was confused at first as I was, looking for weightSum in the ConstraintLayout documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文