2011-09-17 89 views

回答

101

根据文档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为具有用于任何装置,如果直接设定的宽度和高度,这将不会发生正确呈现的布局是有用的。

+130

这不是一个解释'重量的目的Sum'。您示例中的行为与'weightSum'省略相同,实际上weightSum不应在该场景中指定。 [documentation](http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum)说,'weightSum定义最大权重和。如果未指定,则通过添加所有孩子的layout_weight来计算总和。“ –

+3

@JeffAxelrod:”不应该“指定它?为什么?我看不出有任何理由。所以我只是说它不需要指定。 – caw

+14

@ MarcoW:它不应该因为可维护性而被指定。正如Jeff所说,将weightSum设置为等于布局内权重的总和不会产生任何效果,但如果将来有人改变它,可能会导致头痛,因为现在必须找到对布局有不必要的修饰符并改变了。这个答案是不正确的,不应该被接受的答案。 – d370urn3ur

23

documentation说得最好,并包括一个例子,(突出显示我的)。

机器人:weightSum

定义最大重量总和。如果未指定,则总和由 加上所有孩子的layout_weight来计算。这可以用于 实例给单个孩子的总可用空间的50%,由 给它一个layout_weight为0.5,并将weightSum设置为1.0。

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

要做到这一点,你将适用layout_weight 1到每个ImageView,没有对TextView,和2.0的LinearLayout一个weightSum

148

添加到superM的和杰夫的回答,

如果有在LinearLayout中,第一个为1的layout_weight,第二个为2 layout_weight并没有weightSum 2次被指定,默认情况下, weightSum计算为3(孩子的权重之和),第一个视图占用1/3的空间,而第二个视图占2/3。

但是,如果我们将weightSum指定为5,那么第一个将占用1/5的空间,而第二个将占用2/5。因此,总共3/5的空间将被布局占用,其余空置。

+1

我一直在寻找相同的东西,那如果权重没有被定义并且权重被提供给子视图,那该怎么办。 你的答案,它计算自己清除了一切 – DeltaCap

+0

这是真的,建议使用RelativeLayout比布局与weightSum? – Robert

8

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

'可能是浮点值,如“1.2”。 (https://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum) –

19

一些试验之后,我认为算法的LinearLayout是这样的:

假设weightSum被设置为一个值不存在的情况下,在以后讨论

首先,除法。 weightSum由LinearLayout维数中的元素数match_parentfill_parent(例如layout_widthorientation="horizontal")我们将这个值称为每个元素的权重乘数w_m.012的默认值是1.0,所以默认权重乘数是1/n,其中nfill_parent元素的数量; wrap_content元素不参与n

w_m = weightSum/#fill_parent

例如当weightSum为60,并且存在元素时,权重乘数为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与weigths 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(例如,在0-1之间,weightSum = 1之间或在上述示例的0-20之间)的元素推出可视区域。

如果没有指定weightSum,它被计算为所有layout_weight值的总和,包括wrap_content元素!所以layout_weight设置在wrap_content元素上,可以影响它们的扩展。例如。负重量会缩小其他元素。 在fill_parent元素布局之前,将上述公式应用于wrap_content元素,其最大可能扩展是根据包装内容进行扩展。 wrap_content元素将会收缩,然后计算并分配其余fill_parent元素的最大可能扩展。

这可能会导致不直观的结果。这似乎是没有人提到

6

一两件事:让我们说你有一个垂直LinearLayout,所以为了在其内部布局/元/视图中的权重,正常工作100% - 所有的人都必须有layout_height属性(必须存在于您的xml文件中)设置为0dp。似乎任何其他价值会在某些情况下搞砸了。

+1

android:layout_width =“match_parent”通常用来代替0dp –

+0

这是为什么呢?我的意思是说你是对的,它搞乱了,但是将高度设置为0 dp就是我的问题。我想知道原因。 –

1

从开发商documentation

这可用于例如通过给它的0.5一个layout_weight和weightSum设置为1.0给总的可用空间的一个孩子50%

加成@Shubhayu回答

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

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

1

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

14

加权总和就像你想要的一样工作(就像其他答案一样,你不必总结父母布局上的所有权重)。在子视图上指定你想要的重量。 不要忘记指定

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> 

这看起来像

enter image description here

相关问题