当我有以下内容时,它显示四种颜色的顶部布局区域比底部布局区域小得多。
根据this documentation的说法,当你向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>
发布于 2011-01-19 09:39:46
我也有同样的问题。诀窍是不要对要设置权重的控件使用"wrap_content“或"fill_parent”。相反,将layout_height设置为0px (在垂直布局中时),然后子控件将根据权重值进行比例调整。
发布于 2010-06-21 20:05:19
如果您在LinearLayout中使用fill_parent,则布局将占用尽可能多的空间,并且稍后定义的所有布局项都必须处理剩余的空间。
如果您将两个LinearLayouts的高度都设置为wrap_content,则重量应按文档所示工作。
发布于 2012-04-09 08:34:23
我知道这为时已晚,但希望它能帮助人们:
在父级上使用android:weightSum
。这里有一个到dev文档的链接。
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum
在父视图上使用1.0
的android:weightSum
,然后在带有android:layout_weight
的子视图上使用0.x
,以便它们使用该比例的空间。
https://stackoverflow.com/questions/3084185
复制相似问题