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

Dhi*_*dya 154 android android-layout android-layout-weight

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

Shu*_*ayu 169

加上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的空间将被布局占用,其余部分为空.

  • 我一直在寻找相同的东西,如果没有定义权重,并且为子视图提供了权重.你计算的答案是自己清除了一切 (2认同)

sup*_*erM 123

根据文档,android:weightSum定义最大权重总和,并计算为layout_weight未明确指定的所有子项的总和.

让我们考虑一个LinearLayout带有水平方向的示例,其中有3个ImageViews.现在我们希望这些ImageViews总是占用相同的空间.要实现此功能,您可以将layout_weight每个设置ImageView为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"/>
  .....
Run Code Online (Sandbox Code Playgroud)

weightSum 对于为任何设备正确呈现布局非常有用,如果直接设置宽度和高度,则不会发生这种情况.

  • 这不是'weightSum`目的的解释.您的示例中的行为与省略的`weightSum`相同,实际上不应在该场景中指定weightSum.[documentation](http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum)说,`weightSum定义最大权重总和.如果未指定,则通过添加所有子项的layout_weight来计算总和 (141认同)
  • @MarcoW.:由于可维护性,不应指定它.正如杰夫所说,将weightSum设置为等于布局内部权重的总和,什么都不做,但是如果有人在将来改变它可能会导致头痛,因为现在必须找到布局的不必要的修改器并改变了.这个答案是不正确的,不应该是接受的答案. (17认同)
  • 这个想法是你可以将weightSum设置为高于子项总和的数字.这会导致孩子们获得一些但不是全部可用的额外空间.在上面的示例中,您的单个子项将继承可用空间的一半而不是全部空间. (5认同)
  • @JeffAxelrod:"不应该"指定?为什么?我认为没有理由.所以我只想说它不需要指定. (3认同)
  • 没有看到weightSum参数的一个很好的解释,所以从文档:"这可以用于例如通过给予layout_weight为0.5并将weightSum设置为1.0来给单个孩子50%的总可用空间." 请参阅http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum (3认同)

aso*_*uzz 26

权重和完全按照您的意愿工作(就像其他答案一样,您不必总计父布局上的所有权重).在子视图上指定您想要的重量.别忘了指定

android:layout_width="0dp" 
Run Code Online (Sandbox Code Playgroud)

以下是一个例子

    <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>
Run Code Online (Sandbox Code Playgroud)

这看起来像

在此输入图像描述


Jef*_*rod 24

文件说,它最好包括一个例子,(突出矿井).

机器人:weightSum

定义最大权重总和.如果未指定,则通过添加所有子项的layout_weight来计算总和.例如,通过给予layout_weight为0.5并将weightSum设置为1.0,可以使单个子节点占总可用空间的50%.

所以为了纠正superM的例子,假设你有一个LinearLayout水平方向,包含两个ImageViews和一个TextViewwith.您可以将TextView其定义为具有固定大小,并且您希望两者ImageViews平均占用剩余空间.

要做到这一点,你将适用layout_weight1至各ImageView,没有对TextViewweightSum2.0上LinearLayout.


Jen*_*sen 20

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

假设将weightSum其设置为值.稍后将讨论缺席的情况.

首先,除以LinearLayout中weightSum的元素数量match_parent或其中的元素数量fill_parent(例如layout_widthfor orientation="horizontal").我们将此值称为权重乘数w_m对于每个元素.默认值为weightSum1.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.这个余数fill_parent根据它们分布在元素之间layout_weight.

第三,每个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
Run Code Online (Sandbox Code Playgroud)

最小扩展的上限为0.最大扩展的上限为父级,即权重上限为0.

如果元素设置为wrap_content,则首先计算其扩展,并且剩余的扩展将在fill_parent元素之间进行分配.如果weightSum设置,则导致layout_weightwrap_content元素没有影响.但是,wrap_content元素仍然可以被重量低于的元素推出可见区域重量乘数(例如,对于weightSum上述示例,0-1表示= 1或0-20之间).

如果未weightSum指定,则将其计算为所有layout_weight值的总和,包括具有wrap_contentset的元素!因此,layout_weight设置wrap_content元素,可以影响他们的扩张.例如,负重会缩小其他fill_parent元素.在fill_parent布置元素之前,将上述公式应用于wrap_content元素,最大可能的扩展是根据包装内容进行扩展.的wrap_content元件将被收缩,之后对其余的最大可能膨胀fill_parent元件被计算和分配.

这可能导致不直观的结果.


Faa*_*hir 10

如果未指定,则通过添加所有子项的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>
Run Code Online (Sandbox Code Playgroud)


Yoa*_*ein 6

有一件事似乎没有人提到过:假设你有一个垂直方向 LinearLayout,所以为了使布局/元素/视图中的权重100%正确地工作 - 所有这些都必须具有layout_height属性(必须存在于xml中)文件)设置为0dp.在某些情况下,似乎任何其他值都会搞砸.