2

环顾一段时间后,我还没有找到与我的问题相匹配的问题。我的问题是这样的,我有一个ListView其条目坚持以下布局:LinearLayout权重不填充父项

<?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,以及如何纠正?

哦,还有一个感兴趣的最后一点,Eclipse的图形布局预览产生预期的结果,当“match_parent”用于父LinearLayout宽度。只有在实际设备上仿真或使用嵌套的LinearLayouts的内容才能获得宽度包装。我猜这是由于布局在ListView中的使用,但谁知道呢?

谢谢

+0

在布局父项中放置android:weightSum =“4”。在此处了解更多信息http://stackoverflow.com/questions/6203609/android-percentage-layout-height – extmkv 2014-09-10 23:29:04

回答

2

好吧,我现在觉得哑巴;我的怀疑是正确的:问题出在ListView上。

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

+1

将此标记为正确答案,以便在将来帮助其他人!很高兴你解决它 – Phiat 2014-09-10 23:52:12