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的空间将被布局占用,其余部分为空.
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
对于为任何设备正确呈现布局非常有用,如果直接设置宽度和高度,则不会发生这种情况.
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
和一个TextView
with.您可以将TextView
其定义为具有固定大小,并且您希望两者ImageViews
平均占用剩余空间.
要做到这一点,你将适用layout_weight
1至各ImageView
,没有对TextView
和weightSum
2.0上LinearLayout
.
Jen*_*sen 20
经过一些实验,我认为LinearLayout的算法是这样的:
假设将weightSum
其设置为值.稍后将讨论缺席的情况.
首先,除以LinearLayout中weightSum
的元素数量match_parent
或其中的元素数量fill_parent
(例如layout_width
for orientation="horizontal"
).我们将此值称为权重乘数对于每个元素.默认值为
weightSum
1.0,因此默认权重乘数为1/n
,其中n
是fill_parent
元素数; wrap_content
元素没有贡献n
.
例如,当weightSum
为60时,并且存在3个fill_parent
元素,权重乘数为20.权重乘数是例如layout_width
如果属性不存在的默认值.
其次,计算每个元素的最大可能扩展.首先,根据wrap_content
元素的内容计算元素.它们的扩展是从父容器的扩展中扣除的.我们会给剩下的人打电话expansion_remainer
.这个余数fill_parent
根据它们分布在元素之间layout_weight
.
第三,每个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
Run Code Online (Sandbox Code Playgroud)
最小扩展的上限为0.最大扩展的上限为父级,即权重上限为0.
如果元素设置为wrap_content
,则首先计算其扩展,并且剩余的扩展将在fill_parent
元素之间进行分配.如果weightSum
设置,则导致layout_weight
对wrap_content
元素没有影响.但是,wrap_content
元素仍然可以被重量低于的元素推出可见区域(例如,对于
weightSum
上述示例,0-1表示= 1或0-20之间).
如果未weightSum
指定,则将其计算为所有layout_weight
值的总和,包括具有wrap_content
set的元素!因此,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)
有一件事似乎没有人提到过:假设你有一个垂直方向 LinearLayout
,所以为了使布局/元素/视图中的权重100%正确地工作 - 所有这些都必须具有layout_height
属性(必须存在于xml中)文件)设置为0dp
.在某些情况下,似乎任何其他值都会搞砸.
归档时间: |
|
查看次数: |
153930 次 |
最近记录: |