不均匀的LinearLayout权重分配

4
我有一个LinearLayout,其中有四个视图水平布局。第一个和最后一个组件是固定大小的。对于内部的两个视图,我希望它们只共享可用空间的50:50。我将每个视图的权重设置为“1”,但是当视图被布局时,视图的大小取决于它们包含的内容。 以下是我的布局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%,但当名称较长时,描述就会向右移动。为什么?

2个回答

11

如果要考虑权重,布局维度需要为0(零)

<TextView android:id="@+id/name" 
    android:text="Name" 
    android:layout_height="fill_parent"  
    android:layout_width="0dip" 
    android:layout_weight="1" 
    android:textSize="25dip">
</TextView>
我建议将权重总和设置为1(并使用分数)或100。
因此,您可以使用50或.5来代替1作为每个视图的权重值。LinearLayout代码将适用于任何权重总和,但如果您稍后想要使用其他部分修改视图,则会变得更加困难。
另外,如果您未使用相对布局,请删除toRightOf属性。越简单越好。

我刚刚在阅读Google LinearLayout教程时注意到了这个问题。感谢您,已经修复了。 - Spidy
2
从技术上讲,宽度必须全部匹配,不能为零。如果所有宽度都设置为0、200、9000或fill_parent,只要它们匹配,权重就会均匀分配。 - Eric
@Eric - 风险在于您为宽度选择的随机值可能会在未来成为新的LayoutParams常量。您认为他们永远不会创建LayoutParams.MAGIC_LAYOUT = 9000吗?我不会打赌。我想要的是一个LayoutParams.USE_WEIGHT常量。 - slund
@Eric是正确的。而这个答案并不完全正确。只要LinearLayout中所有具有权重的视图具有相同的宽度,它就会是均匀的。 - Patrick Boos
@Patick使用零宽度可以在布局中进行一些优化(避免额外调用子元素的测量)。如果您可以设置大小,使得测量结果精确,则设置实际宽度将有所帮助,否则没有必要。因此,断言大小需要为零可能会过分。我仍然坚持我的答案,因为我认为这是最好的做法。 - slund

1
尝试在LinearLayout的所有子元素中使用android:layout_width="fill_parent"而不是"wrap_content"。或者更好的方法是,在您的xml中创建这样的结构:
<RelativeLayout>
    <ImageView />       # status, fixed width, alignParentLeft="true"
    <TextView />        # time, fixed width, alignParentRight="true"
    <LinearLayout>      # layout_width="fill_parent", toLeftOf="time" toRightOf="status"
        <TextView />    # name, use layout_weight="1"
        <TextView />    # description, use layout_weight="1"
    </LinearLayout>
</RelativeLayout>

这应该可以满足你的需求。使用LinearLayout而不是RelativeLayout也可能有效,但你需要进行一些实验(我相信像我的示例中使用嵌套布局将会起作用)。


我没有尝试过这个,但我认为fill_parent也可以解决这个问题,因为我以前用过。对于这个回答点赞,谢谢。 - Spidy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接