android:layout_weight初学者的问题

当我有以下几点时,显示四种颜色的顶部布局比底部布局区域小得多。

根据http://developer.android.com/guide/topics/ui/layout-objects.html#linearlayout ,当你添加更多的layout_weight,它应该增加面积,但在代码中减less。

任何帮助将不胜感激。

提前致谢。

<?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> 

我有同样的问题。 窍门是不要使用“wrap_content”或“fill_parent”作为您正在设置权重的控件。 相反,将layout_height设置为0px(当在垂直布局中时),然后子控件将按照权重值进行比例分配。

如果你得到的错误

错误

可疑尺寸:这将使视图不可见,可能用于布局…

记得在父母的android:orientation中设置正确的参数

如果在LinearLayout中使用fill_parent,则布局将占用尽可能多的空间,稍后定义的所有布局项目都必须处理剩下的空间。

如果您将LinearLayouts的高度设置为wrap_content,则权重应按文档所述工作。

我知道这是迟到,但希望它可以帮助人们:

在父android:weightSum上使用android:weightSum 。 这是一个链接到开发文档。

http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

在父0.x上使用android:weightSum 1.0 ,然后在android:layout_weight的子视图上使用0.x ,以便他们使用该比例的空间。

基于Janusz的说法,如果你使用fill_parent,你可以设置android:layout_weight来“拆分”多个布局项目之间的“全区域”。

layout_weight不增加面积,它增加了“正确”的区域。 但它也是相对于父母。 如果父级的layout_height = fill_parent,其layout_weight = 0,并且父级具有相同的父级,那么将layout_weight = 1设置为子级之一不会影响父级。

父母和兄弟姐妹都将占用他们可以填补的面积的50%。

解决方法是使用layout_weight时,将layout_height设置为0px。

但是你为什么会观察到这种显然是奇怪的/颠倒的行为?

不久之后 :剩下的空间是负面的,所以体重4的孩子会得到更多的负面空间,并且身高也会减less。

详情

假设您的父级垂直布局的高度是100像素。

每个孩子的布局高度是fill_parent (即每个孩子也是100像素的高度)

所有孩子的总高度= 2 * 100像素

剩余的高度= 100 – (2 * 100)= – 100像素( 它是负的

现在,让我们分配这个孩子之间的剩余高度。 第一个是80%(即-100 * 4 /(4 + 1))。 第二个孩子得到20%(即-100 * 1 /(4 + 1))

现在计算结果的高度:

孩子1:100 +(-80)= 20px

孩子2:100 +(-20)= 80px

这里没什么奇怪的,只有math。 请注意,剩余的空间可能是负面的! (或者build议将高度显式设置为0)

在线性布局属性中,将布局宽度更改为"fill_parent"而不是"wrap_content"希望它"fill_parent"帮助。

如果linearlayout的方向是垂直的,则将layout_height设置为0dp。 在水平布局的情况下,将layout_width设置为0dp。

如果我们不遵循上面的规则,这个观点往往会按照特定的属性来占据空间,因此这个alignment不是按照预期的。