android:layout_weight 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3084185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does android:layout_weight work?
提问by shin
When I have the following, it shows top layout with four colors has much smaller area than the bottom layout area.
当我有以下内容时,它显示四种颜色的顶部布局比底部布局区域小得多。
According to this documentation, when you add more to layout_weight
, it should increase the area, but it decreases in the code.
根据这个文档,当你添加更多时layout_weight
,它应该增加面积,但它会减少代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="3"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
回答by kbriggs
I had the same problem. The trick is not to use "wrap_content" or "fill_parent" for the controls you are setting a weight to. Instead set the layout_height to 0px (when inside a vertical layout) and then the child controls will get proportioned per the weight values.
我有同样的问题。诀窍是不要对要设置权重的控件使用“wrap_content”或“fill_parent”。而是将 layout_height 设置为 0px(在垂直布局内时),然后子控件将根据权重值按比例分配。
回答by pedroca
If you get the error as
如果你得到的错误是
error
错误
Suspicious size: this will make the view invisible, probably intended for layout ...
可疑大小:这将使视图不可见,可能用于布局......
remember to set the correct parameter on
android:orientation
in parent
记得android:orientation
在父级中设置正确的参数
回答by Janusz
If you are using fill_parent in a LinearLayout the layout will take as much space as possible and all layout items defined later will have to deal with the space left.
如果您在 LinearLayout 中使用 fill_parent,则布局将占用尽可能多的空间,并且稍后定义的所有布局项都必须处理剩余的空间。
If you set the height of both of you LinearLayouts to wrap_content the weight should work as documented.
如果您将两个 LinearLayouts 的高度设置为 wrap_content,则重量应该按照文档中的说明工作。
回答by trgraglia
I know this is late but hopefully it helps people:
我知道这已经晚了,但希望它可以帮助人们:
Use android:weightSum
on the parent. Here is a link to the dev docs.
android:weightSum
在父级上使用。这是开发文档的链接。
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum
Use android:weightSum
of 1.0
on the parent and then use 0.x
on the child views with android:layout_weight
in order for them to use that ratio of space.
使用android:weightSum
的1.0
父,然后用0.x
对孩子的看法与android:layout_weight
以便他们使用的空间比例。
回答by Ryan Conrad
Building on what Janusz said, if you use fill_parent, you can then set android:layout_weight to "split" the "full area" between multiple layout items.
基于 Janusz 所说的,如果您使用 fill_parent,则可以将 android:layout_weight 设置为“拆分”多个布局项之间的“完整区域”。
The layout_weight doesn't increase the area, it increases it "right" to the area. but it's also relative to the parent. If you have a parent with a layout_height=fill_parent, with a layout_weight=0 and the parent has a sibling with the same, setting layout_weight=1 to one of the children does not affect the parent.
layout_weight 不会增加区域,而是将其“正确”增加到该区域。但它也是相对于父级的。如果您有一个 layout_height=fill_parent 的父级,layout_weight=0 并且父级有一个相同的兄弟,则将 layout_weight=1 设置为其中一个子级不会影响父级。
Both the parent, and the sibling, would take up 50% of the available area that they can fill.
父级和兄弟级都将占据他们可以填充的可用区域的 50%。
回答by ben75
The solution is to set layout_height to 0px when you use layout_weight.
解决方法是在使用layout_weight时将layout_height设置为0px。
But why do you observe this apparently strange/inversed behavior ?
但是为什么您会观察到这种明显奇怪/相反的行为?
Shortly: the remaining space is negative and so the child with weight 4 receive more negative space and it's height is more reduced.
很快:剩余空间为负,因此体重为 4 的孩子获得更多的负空间,并且高度降低更多。
Details:
详情:
Assume that the height of your parent vertical layout is 100 pixels.
假设父垂直布局的高度为 100 像素。
Layout height on each child is fill_parent
(i.e. each child is also 100 pixels height)
每个孩子的布局高度是fill_parent
(即每个孩子也是 100 像素高度)
The total height of all child = 2*100 pixels
所有孩子的总高度 = 2*100 像素
The remaining height = 100 - (2*100) = -100 pixels (it is negative)
剩余高度 = 100 - (2*100) = -100 像素(为负)
Now, let's distribute this remaining height between child. The first one will receive the biggest part : 80% (i.e. -100*4/(4+1)). The second child receive 20% (i.e. -100*1/(4+1))
现在,让我们在孩子之间分配这个剩余的高度。第一个将获得最大的部分:80%(即 -100*4/(4+1))。第二个孩子得到20%(即-100*1/(4+1))
Now compute the resulting height :
现在计算结果高度:
child 1 : 100 + (-80) = 20px
孩子 1 : 100 + (-80) = 20px
child 2 : 100 + (-20) = 80px
孩子 2 : 100 + (-20) = 80px
Nothing strange here, only mathematics. Just be aware that remaining space can be negative ! (or set the height explicitly at 0 as it is recommended)
这里没什么奇怪的,只有数学。请注意,剩余空间可能为负值!(或按照建议将高度明确设置为 0)
回答by Shinoo Goyal
If the orientation of linearlayout is vertical,then set layout_height as 0dp. In case of horizontal layout set layout_width as 0dp.
如果linearlayout的方向是垂直的,则设置layout_height为0dp。在水平布局的情况下,将 layout_width 设置为 0dp。
If we do not follow above rules,the view tends to take up space as per specified attributes and hence the alignment is not as per expectation.
如果我们不遵循上述规则,视图往往会根据指定的属性占用空间,因此对齐方式不符合预期。
回答by logeshps
In linear layout properties change the layout width to "fill_parent"
instead of "wrap_content"
hope it helps.
在线性布局属性中,将布局宽度更改为"fill_parent"
而不是"wrap_content"
希望它有所帮助。