LineayrLayout之属性weight

使用LinearLayout时经常会使用weight来适配一些布局。下面做下总结:

父类LinearLayout Xml如下:

<LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >

情况一:当子view的width="wrap_content"时:


<LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal" >
       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="" >
       </Button>
       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="" >
       </Button>
       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="3"
           android:text="" >
       </Button>
   </LinearLayout>

效果如下:

spacer.gif103130567.jpg

即实际屏幕效果比例与权重比,成正比例。但既然是wrap_content,所以即使button3的weigth=100,前面两个button的屏幕比例也不会变的很小,至少都是wrap_content。如下图:

103530273.jpg


情况二:当子view的width="fill_parent"时:


分两种情况,当子view只有两个时:权重比与实际屏幕比例成反比。

即当view1和view2的 weight比是1:2,实际屏幕比例为2:1,

效果如下如:

104718606.jpg


当子view个数大于两个时权重比与实际屏幕比例,计算如下:

如view1 weight= 1 ;

view2 weight= 2 ;

view3 weight= 3 ;

实际屏幕比例为 1 - 2*weigth/weigthSum ;

即view1 = 1-2*1/6 = 2/3 ;

view2 = 1-2*2/6 = 1/3 ;

view3 = 1-2*3/6 = -1/3 ;

所以view3不显示,效果如下图:

105445284.jpg


为什么为这样呢?需要查看源码,LinearyLayout的measureHorizontal方法,当

把子组件的属性改为fill_parent之后,子组件的宽度被计算了两次.

若有2个组件, 权值分别为1 , 2, 设屏幕宽度为X,则计算如下:

第一次计算,两个组件的宽度都为x(因为fill);

Delta = x - 2x(所有组件宽度之和) = -x;

X1 = x+ share1 = x + (delta * 1 / 3) = 2/3x(第一次计算的值和剩余的相加)

X2 = x+ share2 = x + (delta * 2 / 3) = 1/3x


若有3个组件,权值分别为 1,2,3 则计算如下:

Delta = x - 3x = -2x;

X1 = x + share1 = x + (-1/6 * 2x) = 2/3x

X2 = 1/3x

X3 = -1/3x




但实际中,一般是根据UI做布局,所以我们希望先得到控件的实际屏幕比例,反过来计算其weight大小。

如希望view1,view2,view3,view4 的屏幕比是1:1:1:3,则其weigth计算如下:

先得到比例:1/6:1/6:1/6:3/6 ;

然后分母减分子得到weigth值:5:5:5:3 ;

代码如下:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text="1" >
    </Button>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text="1" >
    </Button>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text="1" >
    </Button>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="3" >
    </Button>
</LinearLayout>

效果如下:

110212678.jpg






情况三:当子view的width="0dp"时:

当其他child View的width=wrap_parent时,该控件会自动填充剩余所有空间。



最后,在 LinearLayout中使用 layout_weight(除非你使用正确), 这样所有子组件的尺寸会被计算两次,将影响性能。

以上为个人经验总结,可能有错误,欢迎留言,指教,讨论。

你可能感兴趣的:(属性,weight,LineayrLayout)