2013-04-18 42 views
7

奇怪的重量反转这是XML布局我工作:安卓:在的LinearLayout

<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="1"设置好的了滚动与layout_weight="2"和的LinearLayout(孩子)我预计会滚动型使用LinearLayout的两倍空闲空间。但是我得到了相反的结果。 ScrollView比LinearLayout小。 而如果我为ScrollView layout_weight="1"和LinearLayout设置了layout_weight="2",则ScrollView大于LinearLayou。

这怎么可能?

回答

15

由于您使用match_parent作为layout_height,因此重量相反。当总和大于LinearLayout时,重量用于分配剩余的空白空间或占用空间。设置你的身高0dip它会工作。

即, 集合layout_height = "0dip"对于ScrollView和内部LinearLayout

参考:

The use of layout_weight with Android layouts