我想知道:什么是android:weightSum和布局权重,以及它们是如何工作的?Android中的android:weightSum是什么,它是如何工作的?
回答
根据文档android:weightSum
定义了最大权重总和,如果未明确指定,则按照所有子项的layout_weight
的和来计算。
让我们考虑一个示例,其中水平方向的LinearLayout
和其中的3 ImageViews
。现在我们希望这些ImageViews
总是占用相同的空间。为了达到这个目的,你可以设置每个ImageView
的layout_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
为具有用于任何装置,如果直接设定的宽度和高度,这将不会发生正确呈现的布局是有用的。
这不是一个解释'重量的目的Sum'。您示例中的行为与'weightSum'省略相同,实际上weightSum不应在该场景中指定。 [documentation](http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum)说,'weightSum定义最大权重和。如果未指定,则通过添加所有孩子的layout_weight来计算总和。“ –
@JeffAxelrod:”不应该“指定它?为什么?我看不出有任何理由。所以我只是说它不需要指定。 – caw
@ MarcoW:它不应该因为可维护性而被指定。正如Jeff所说,将weightSum设置为等于布局内权重的总和不会产生任何效果,但如果将来有人改变它,可能会导致头痛,因为现在必须找到对布局有不必要的修饰符并改变了。这个答案是不正确的,不应该被接受的答案。 – d370urn3ur
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
。
添加到superM的和杰夫的回答,
如果有在LinearLayout中,第一个为1的layout_weight,第二个为2 layout_weight并没有weightSum 2次被指定,默认情况下, weightSum计算为3(孩子的权重之和),第一个视图占用1/3的空间,而第二个视图占2/3。
但是,如果我们将weightSum指定为5,那么第一个将占用1/5的空间,而第二个将占用2/5。因此,总共3/5的空间将被布局占用,其余空置。
如果未指定,则通过添加所有孩子的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>
'可能是浮点值,如“1.2”。 (https://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum) –
一些试验之后,我认为算法的LinearLayout是这样的:
假设weightSum
被设置为一个值不存在的情况下,在以后讨论
首先,除法。 weightSum
由LinearLayout维数中的元素数match_parent
或fill_parent
(例如layout_width
为orientation="horizontal"
)我们将这个值称为每个元素的权重乘数.012的默认值是1.0,所以默认权重乘数是1/n
,其中n
是fill_parent
元素的数量; wrap_content
元素不参与n
。
例如当weightSum
为60,并且存在元素时,权重乘数为20.权重乘数是例如如果属性不存在,则为layout_width
。
其次,计算每个元素的最大可能扩展。首先,根据元素的内容计算wrap_content
元素。它们的扩展从父容器的扩展中扣除。我们将称其为剩余物expansion_remainer
。根据其layout_weight
,该余数分布在fill_parent
元素中。
第三,每fill_parent
元件的膨胀被计算为:
实施例:
如果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_weight
对wrap_content
元素没有影响。然而,wrap_content
元素仍然可以被重量低于(例如,在0-1之间,weightSum
= 1之间或在上述示例的0-20之间)的元素推出可视区域。
如果没有指定weightSum
,它被计算为所有layout_weight
值的总和,包括wrap_content
集元素!所以layout_weight
设置在wrap_content
元素上,可以影响它们的扩展。例如。负重量会缩小其他元素。 在fill_parent
元素布局之前,将上述公式应用于wrap_content
元素,其最大可能扩展是根据包装内容进行扩展。 wrap_content
元素将会收缩,然后计算并分配其余fill_parent
元素的最大可能扩展。
这可能会导致不直观的结果。这似乎是没有人提到
一两件事:让我们说你有一个垂直LinearLayout
,所以为了在其内部布局/元/视图中的权重,正常工作100% - 所有的人都必须有layout_height
属性(必须存在于您的xml文件中)设置为0dp
。似乎任何其他价值会在某些情况下搞砸了。
android:layout_width =“match_parent”通常用来代替0dp –
这是为什么呢?我的意思是说你是对的,它搞乱了,但是将高度设置为0 dp就是我的问题。我想知道原因。 –
从开发商documentation
这可用于例如通过给它的0.5
一个layout_weight和weightSum设置为1.0
给总的可用空间的一个孩子50%
。
加成@Shubhayu回答
休息3/5
可用于这真的不需要包含布局的任何特定部分其他子布局。
这是android:weightSum
属性的潜在用途。
布局权重就像一个比例。例如,如果存在垂直布局并且存在两个项目(例如按钮或文字浏览),则布局权重为2而另一个分别具有布局权重3。然后,第1项将占据屏幕/布局的5部分中的2部分,而5部分中的另外3部分占据3部分。这里是5的重量总和。即权重和将整个布局划分为定义的部分。 并且布局权重定义特定项目在预先定义的总权重总和中占多少部分。重量总和也可以手动声明。当使用UI设计的线性布局时,按钮,文字浏览,编辑文本等全部使用权重和布局权重进行组织。
加权总和就像你想要的一样工作(就像其他答案一样,你不必总结父母布局上的所有权重)。在子视图上指定你想要的重量。 不要忘记指定
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>
这看起来像
- 1. Jenkins的种子工作是什么?它是如何工作的?
- 2. 什么是IOR文件,它是做什么的,它是如何工作的?
- 3. 什么是延迟后台工作?它是如何工作的?
- 4. 什么是Wicket中的ResourceReferences,它们是如何工作的?
- 5. 什么是SQL中的binary_checksum?它是如何工作的?
- 6. PreparedStatement缓存 - 它是什么意思(它是如何工作的)
- 7. Ext.net PageProxy:它是什么,它是如何工作的?
- 8. 在Ruby中“=>”是什么意思,它是如何工作的?
- 9. 什么是复制的指针,它们是如何工作的?
- 10. 什么是懒惰的策略,它是如何工作的?
- 11. promisifyAll如何工作,或者它的工作要求是什么?
- 12. 什么preprocessing.scale()做什么?它是如何工作的?
- 13. EnableEventValidation的用途是什么?它的工作原理是什么?
- 14. 什么是print << EOF;它是如何工作的?
- 15. 什么是autoresizesForKeyboard,它是如何工作的?
- 16. 什么是Microsoft消息队列(MSMQ)?它是如何工作的?
- 17. 什么是[cmdletbinding()],它是如何工作的?
- 18. 这个网址是什么意思,它是如何工作的?
- 19. 这是什么语法结构,它是如何工作的?
- 20. 什么是参考计数器,它是如何工作的?
- 21. 什么是Equals Operator?它是如何工作的?
- 22. 什么是SSH,它是如何工作的?
- 23. 什么是cron文件?它是如何工作的?
- 24. 什么是Windows套件,它们是如何工作的?
- 25. 什么是多线程程序,它是如何工作的?
- 26. 什么是视图...视图?它是如何工作的?
- 27. QML - 什么是id,它是如何工作的?
- 28. 什么是“do_shortcode”在WordPress和它是如何工作的?
- 29. 什么是Maybe类型?它是如何工作的?
- 30. 什么是SVG,它是如何工作的?
http://stackoverflow.com/questions/7452741/什么是androidweightsum在安卓和如何做它的工作/ 38062959#38062959 –