【问题标题】:Uneven LinearLayout weight distribution不均匀的 LinearLayout 权重分布
【发布时间】:2011-05-25 06:09:31
【问题描述】:

我有一个 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%,但是当名称较长时,描述会向右移动。为什么?

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    对于要考虑的权重,布局维度需要为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 或 0.5 而不是 1。 LinearLayout 代码适用于任何权重总和,但如果您想稍后通过其他部分修改您的视图,这将变得很困难。

    另外,如果您不使用相对布局,请去掉 toRightOf 属性。少即是多。

    【讨论】:

    • 我在阅读 Google LinearLayout 教程时注意到了这一点。非常感谢,已修复
    • 从技术上讲,宽度必须完全匹配,而不是为零。如果所有宽度都设置为 0、200、9000 或 fill_parent,只要它们匹配,就会均匀分布权重。
    • @Eric - 存在的危险是,您为宽度选择的随机值将来可能会成为新的 LayoutParams 常量。认为他们永远不会让 LayoutParams.MAGIC_LAYOUT = 9000?我不会打赌。我想要的是一个 LayoutParams.USE_WEIGHT 常量。
    • @Eric 是正确的。这个答案并不正确。只要 LinearLayout 中所有具有权重的视图都具有相同的宽度,它就会是均匀的。
    • @Patick 使用零宽度允许对布局进行一些优化(避免额外调用来测量子级)。如果您可以设置尺寸以使测量结果准确,那么设置实际宽度会有所帮助,否则就没有必要了。因此,断言大小需要为零可能是过分的。我仍然坚持这个答案,因为我认为这是最好的做法。
    【解决方案2】:

    尝试在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 也会解决这个问题,因为我以前用过它。 +1 回复,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多