我有一个LinearLayout有四个视图水平放置。第一个和最后一个组件是一个集合大小。对于内部的两个视图,我想分享可用空间50:50。我将每个设置为“1”的权重,但是当视图放置时,视图根据其内容的不同而不同。 不平衡LinearLayout重量分布
这是我的布局xml供参考。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/status"
android:src="@drawable/white"
android:paddingRight="10dip"
android:layout_height="35dip"
android:layout_width="35dip">
</ImageView>
<TextView android:id="@+id/name"
android:text="Name"
android:layout_height="fill_parent"
android:layout_toRightOf="@id/status"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textSize="25dip">
</TextView>
<TextView android:id="@+id/description"
android:text="Description"
android:layout_toRightOf="@id/name"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textSize="25dip">
</TextView>
<TextView android:id="@+id/time"
android:text="Time"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="@id/description"
android:textSize="25dip">
</TextView>
</LinearLayout>
显然这些不是实际的列名,但我为了隐私目的更改了它们。这个布局被ListView使用,它将每个视图的文本改变为它呈现的任何值。名称和说明字段应该对齐,因为它们都是剩余屏幕的50%,但当名称更长时,说明会右移。为什么?
我刚刚注意到,在阅读谷歌LinearLayout教程。谢谢你,修复它 – Spidy 2011-05-24 22:14:46
从技术上讲,宽度必须全部匹配,而不是零。如果所有宽度都设置为0,200,9000或fill_parent,只要它们匹配,重量将均匀分配。 – Eric 2011-05-24 23:09:57
@Eric - 存在的危险是,您为宽度选择的随机值可能会成为未来的新LayoutParams常量。认为他们永远不会让LayoutParams.MAGIC_LAYOUT = 9000?我不打赌。我想要的是一个LayoutParams.USE_WEIGHT常量。 – slund 2011-05-25 13:38:57