Android:LinearLayout 中奇怪的权重倒置

14

这是我正在工作的XML布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <ScrollView 
        android:layout_weight="2" 
        android:id="@+id/scrollConfirm" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

  </ScrollView>

  <LinearLayout 
      android:layout_marginTop="20px"
      android:layout_weight="1" 
      android:id="@+id/imageNumpad" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent">
        <ImageView android:src="@drawable/myicon"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#FFFFFF"
        />
   </LinearLayout>
</LinearLayout>

由于我已经设置了具有layout_weight="2"的ScrollView和具有layout_weight="1"的LinearLayout(子项),因此我预计ScrollView将使用比LinearLayout多两倍的可用空间。但是我得到了相反的结果。ScrollView比LinearLayout更小。

然而,如果我为ScrollView设置layout_weight="1",并且为LinearLayout设置layout_weight="2",则ScrollView大于LinearLayout。

这是怎么可能的??

1个回答

26
由于您使用了match_parent作为layout_height,导致权重变得无效。 权重用于分配剩余空间或在总和大于LinearLayout时取走空间。将高度设置为0dip即可解决问题。

例如,将ScrollView和内部的LinearLayoutlayout_height都设置为"0dip"

参考资料:

The use of layout_weight with Android layouts


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