【问题标题】:What is android:weightSum in android, and how does it work?什么是android中的android:weightSum,它是如何工作的?
【发布时间】:2011-11-19 03:29:47
【问题描述】:

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

【问题讨论】:

    标签: android android-layout android-layout-weight


    【解决方案1】:

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

    【讨论】:

    • 我一直在寻找相同的方法,即如果未定义 weightsum 并且为子视图提供权重会怎样。你自己计算的答案自己清除了一切
    • 推荐使用RelativeLayout比使用weightSum的Layouts是真的吗?
    【解决方案2】:

    根据文档,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 有助于为任何设备正确呈现布局,如果您直接设置宽度和高度,则不会发生这种情况。

    【讨论】:

    • 这不是对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.
    • @JeffAxelrod:“不应该”指定?为什么?我看不出有什么理由。所以我只想说它不需要指定。
    • @MarcoW.:由于可维护性,不应指定它。正如 Jeff 所说,将 weightSum 设置为等于布局内权重的总和没有任何作用,但是如果将来有人更改它可能会引起头痛,因为现在必须找到对布局的不必要修改并改变了。此答案不正确,不应被接受。
    • 看不到对 weightSum 参数的很好解释,因此来自文档:“这可以用于例如通过给它一个 layout_weight 0.5 和将 weightSum 设置为 1.0。”见developer.android.com/reference/android/widget/…
    • 这个想法是您可以将 weightSum 设置为高于子项总和的数字。这会导致孩子们获得一些但不是全部可用的额外空间。在上面的示例中,您的单个孩子将继承一半的可用空间,而不是全部。
    【解决方案3】:

    权重总和完全按照您的意愿工作(与其他答案一样,您不必对父布局上的所有权重求和)。在子视图上指定您希望它承受的重量。 别忘了指明

    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>
    

    这看起来像

    【讨论】:

      【解决方案4】:

      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

      【讨论】:

        【解决方案5】:

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

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

        首先,将weightSum 除以LinearLayout 维度中match_parentfill_parent 的元素数(例如layout_width 对应orientation="horizontal")。我们将此值称为每个元素的权重乘数 。 weightSum 的默认值为 1.0,因此默认权重乘数为1/n,其中nfill_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_weightwrap_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 元素的最大可能扩展。

        这可能会导致不直观的结果。

        【讨论】:

          【解决方案6】:

          如果未指定,则通过添加所有子项的 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>
          

          【讨论】:

          【解决方案7】:

          似乎没有其他人提到过的一件事:假设您有一个 垂直 LinearLayout,因此为了使其内部布局/元素/视图中的权重能够 100% 正常工作 -它们都必须将 layout_height 属性(必须存在于您的 xml 文件中)设置为 0dp。在某些情况下,似乎任何其他值都会把事情搞砸。

          【讨论】:

          • android:layout_width="match_parent" 通常用来代替 0dp
          • 为什么会这样?我的意思是你是对的,它搞砸了,但是将高度设置为 0 dp 解决了我的问题。我想知道原因。
          【解决方案8】:

          布局权重就像一个比率。例如,如果有一个垂直布局并且有两个项目(例如按钮或文本视图),一个具有布局权重 2,另一个具有布局权重3分别。然后第一项将占据屏幕/布局的 5 部分中的 2 部分,而另一部分将占据 5 部分中的 3 部分。这里5是重量总和。即 权重总和将整个布局划分为定义的部分。 并且布局重量定义了特定项目在预定义的总重量总和中所占的比例。重量总和也可以手动声明。在 UI 设计中使用线性布局时,按钮、文本视图、编辑文本等都使用 weightsum 和布局权重进行组织。

          【讨论】:

            【解决方案9】:

            来自开发者documentation

            这可以用于例如通过将 layout_weight 设置为 0.5 并将 weightSum 设置为 1.0 来将总可用空间的单个子 50% 分配给它。

            添加到@Shubhayu 答案

            rest 3/5 可用于其他真正不需要包含布局的任何特定部分的子布局。

            这是android:weightSum 属性的潜在用途。

            【讨论】:

              【解决方案10】:

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

              我相信这对像我一样一开始感到困惑的人会有所帮助,在 ConstraintLayout 文档中寻找 weightSum

              【讨论】:

                猜你喜欢
                • 2013-02-13
                • 2018-06-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-03-29
                • 2012-11-06
                • 2018-09-05
                相关资源
                最近更新 更多