weight属性你用的真的6嘛?

  相信大家在日常开发中一定使用过weight这个属性,它的作用一个是权重,另一个就是渲染优先级,但是你真的能很6的使用它嘛?如果不是,那么请继续往下看!!!

  我们知道,当weight起到不同作用的时候,weight属性的值可以影响控件的效果。但是具体的影响是怎么样的哪?

  当起权重作用的时候,weight属性值越大,占据的空间越大。(要求weight属性必须和0dp一起使用)

  当起渲染优先级作用的时候,weight属性值越大,越后渲染。

  其实,所谓占据空间大小是根据下面这个公式算出来的:

    实际宽(高)度 = 原来宽(高)度 + 剩余空间 * weight所占比重

    注意:不写weight属性的时候,默认weight属性值为0,并且只有Linearlayout本身及其子控件才能使用这个属性。

  那么下面就让我根据实际的例子,带大家熟悉一下我们这个公式可爱的地方😄。

 

1  权重

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="weight=1"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="weight=2"/>

</LinearLayout>

    效果图:  

       

    公式计算:

      btn1: 0 + (L - 0 - 0) * 1/3 = 1/3 L

      btn2: 0 + (L - 0 - 0)  * 2/3 = 2/3L

    结论:此时weight的作用是权重,weight属性值越大,占据的空间越大

       上面0dp改成match_parent效果相反的

      

     

2  渲染优先级

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="weight=1"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="weight=2"/>

    </LinearLayout>

  效果图:

    

  公式计算:

    btn1:L  + (L - 2L) * 1/3 = L - 1/3L = 2/3L

      btn2:L  + (L - 2L) * 2/3 = L - 1/3L = 1/3L

  结论:此时weight的作用是渲染优先级,weight属性值越大,越后渲染。(这里可能对越后渲染这个概念解析的不到位,具体请看最后一个例子)

 

   那么怎么区别这两个作用那?当weight属性和宽或高(具体取决于你想使用权重的方向)属性值为0dp使用的时候,weight起到的就是权重的作用。否则就是渲染优先级的作用。上面是weight属性两个最常见的使用方法,难道了解这些就可以了嘛?当然不是,下面给大家列举一下weight的其它几种用法,目的在于让大家更加深入的理解上面那个可爱的公式。

 

1  weight和wrap_content、wrap_content一起使用

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="weight=1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="weight=2"/>

    </LinearLayout>

    效果图:

      

    公式计算:

      btn1:wr1 + ( L - wr1 - wr2) * 1/3 

      btn2:wr2 + ( L - wr1 - wr2) * 2/3

 

2  weight和wrap_content、match_parent一起使用

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="weight=1"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="weight=2"/>

    </LinearLayout>

    效果图:

      

    计算公式:

       btn1:wr1 + (L - wr1 - L) * 1/3 = 2/3wr1

       btn2:L + (L - wr1 - L) * 2/3 = L - 2/3wr1

 

3  weight和wrap_content、0dp一起使用

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="weight=1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="weight=2"/>

</LinearLayout>

    效果图:

       

    计算公式:

      btn1:0 + (L - 0 - wr2) * 1/3

      btn2:wr2 + (L - 0 - wr2) * 2/3

 

4  weight和match_parent、0dp一起使用

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="weight=1"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center_horizontal"
            android:text="weight=2"/>

</LinearLayout>

    效果图:btn2超出屏幕一部分

    

    计算公式:

        btn1:0 + (L - 0 - L) * 1/3 = 0     近似等于包裹内容(此处我也想不太明白,欢迎指正!!!)

      btn2:L + (L - 0 - L) * 2/3 = L

    结论:这个例子很好的说明了weight渲染优先级的作用,weight属性值越大,越后渲染,因为如果把上面btn1和btn2的代码换个位置效果图是这样的:

    ,我们看到btn1超出屏幕渲染,当然也有可能是btn1和btn2的父布局设置为水平导致的,具体说明原因我也不是很清楚,欢迎指正!!!

    在LinearLayout中还有一个weightSum属性,顾名思义,就是指定总权重的,还记得上面各个效果图的计算公式1/3、2/3嘛?这里面的分母是我们1+2得到的,但是当你使用weightSum这个属性的时候,那么此时的分母值就是weightSum的属性值。That's All !!!,有关weight的使用方法到此就结束了,这个本人的第一篇博客,说是博客也好,说是笔记也好,总之如果对你们有用,就欢迎阅读、欢迎指正。

posted @ 2016-11-12 15:26  梦一场yu  阅读(967)  评论(0编辑  收藏  举报