【问题标题】:LinearLayout weights not filling parentLinearLayout 权重不填充父级
【发布时间】:2014-09-10 23:25:16
【问题描述】:

环顾了一段时间后,我还没有找到与我的问题相匹配的问题。我的问题是,我有一个 ListView,其条目遵循以下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants" >

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        amdroid:layout_weight="1"
        android:orientation="vertical" >
        ...
    </LinearLayout>

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical" >
        ...
    </LinearLayout>

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
        ...
    </LinearLayout>
</LinearLayout>

我希望这会产生一个条目,其中第一个嵌套布局获得可用宽度的 1/4,第二个获得宽度的 1/2,第三个获得宽度的 1/4。但这不是发生的事情。相反,由于某种原因,每个嵌套布局的宽度都会被换行。

有趣的是,如果我在父 LinearLayout 中指定特定宽度而不是“match_parent”,则嵌套布局宽度会按预期服从权重。例如,以下会产生预期的结果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="290dp"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants" >

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        amdroid:layout_weight="1"
        android:orientation="vertical" >
        ...
    </LinearLayout>

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical" >
        ...
    </LinearLayout>

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
        ...
    </LinearLayout>
</LinearLayout>

但显然,指定特定宽度会忽略整个权重点。所以我的问题是:为什么使用“match_parent”时权重实际上没有填充父LinearLayout,如何纠正?

还有一个有趣的地方,当“match_parent”用于父 LinearLayout 宽度时,Eclipse 中的图形布局预览会产生预期的结果。只有在实际设备上模拟或使用时,嵌套的 LinearLayout 的内容才会被宽度环绕。我猜这是因为布局在 ListView 中的使用,但谁知道呢?

谢谢

【问题讨论】:

标签: android xml android-layout android-linearlayout android-layout-weight


【解决方案1】:

好吧,我现在感觉很笨;我的怀疑是正确的:问题出在 ListView 上。

我在实例化 ListView 的布局中为 ListView 宽度指定了“wrap_content”。通过指定“match_parent”更正 ListView 宽度会为条目布局生成预期结果,而不依赖于硬编码的 layout_width。

【讨论】:

  • 将此标记为正确答案,以便将来帮助其他人!很高兴你修好了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
相关资源
最近更新 更多