Android layout_weight 理解

        从事android开发一段时间了,经常需要使用Android LinearLayout的layout_weight属性。我平时使用的时候都是想当然,大概达到自己布局的效果就行了,从来没有好好地研究过。今天和同事讨论,发现自己的认识还不够,上网查找资料,不如自己去尝试下,故把自己的理解记录下来~

  首先,通过查找资料,结合在网上看别人的文章,有一句话很重要:首先按照控件中声明的尺寸进行分配,然后剩下的尺寸按照weight进行分配,最后全部加到我们相应的控件上去。

   一开始,我新建了一个项目,而且这个项目只是一个LinearLayout的展现,如图所示:

        

  这是两个一红一蓝两个布局对我们的屏幕进行了均分,那么我的布局代码如下:

           

<span style="font-family:SimSun;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/layout1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:background="#0072e3"
        android:gravity="center" />

    <TextView
        android:id="@+id/layout2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:background="#ff2d2d"
        android:gravity="center" />
</LinearLayout></span>


   这是一个很普通的布局,在横向排布的线性布局内部有两个TextView,它们的width都是0dp,weight是相同的,所以对整个布局进行了均分,就体现为上图的样子。

        

  那么,如果我们对width这个属性进行设置的话,我的布局会产生什么样的变化呢? 比如我随便把layout1布局的宽度设置成100dp,layout2的布局依旧是0dp,代码如下:

        

<span style="font-family:SimSun;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/layout1"
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:background="#0072e3"
        android:gravity="center" />

    <TextView
        android:id="@+id/layout2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:background="#ff2d2d"
        android:gravity="center" />
</LinearLayout></span>

 我的布局效果相应是:

         


  在这里呢,layout_weight的值都是1.0,但是他们的宽度已经不一样了,这是由于我设置了layout1的宽度为100dp,也就是我们根据上面的红字,先给layout1分配100dp,layout2分配0dp,那么剩下的父布局的空间由layout1和layout2均分,就造成了上面这张图的效果。

  好的,以前我发现有的时候在线性布局里使用layout_weight属性的时候,有时layout_weight越大,相应控件所分配到的空间越大,有的时候却恰恰相反。在这里我们可以试试这个例子:代码调整如下:


       

<span style="font-family:SimSun;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/layout1"
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:layout_weight="2.0"
        android:background="#0072e3"
        android:gravity="center" />

    <TextView
        android:id="@+id/layout2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:background="#ff2d2d"
        android:gravity="center" />
</LinearLayout></span>
        

  我把layout1的布局设置成了layout_width为100dp,layout_width为2.0;layout2的layout_width为fill_parent,layout_width为1.0,这里我的效果是下图:

      

      


  这个曾经的我是弄不清楚的,那么怎么根据代码来分析这个效果呢?我们先按照控件的尺寸进行分配,假设我的屏幕的宽度是360dp(drawable-xhdpi:  屏幕密度为320的手机设备,因此我的width在这里是360dp),那么layout1分配到100dp,layout2根据fill_parent分配得到360dp,那么屏幕剩下的尺寸就是360dp-(360dp+100dp)= -100dp,我们根据layout_weight的比例来进行分析,layout1分配得到约-66dp,layout2分配得到约-33dp。那么加上之前已经分配的,layout1最后占据的区域是100dp-66dp=34dp,layout2占据的区域是360dp-33dp为327dp,也就是我上图所示的效果。

  其实说了这么多,只是从逻辑层面去计算了我们在不同情况下控件所分配到的区域的大小。那么我在使用的时候,可以参考我们的android sdk的话:In order to improve the layout efficiency when you specify the weight, you should change the width of the EditText to be zero (0dp). Setting the width to zero improves layout performance because using "wrap_content"as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.  也就是说我们如果使用了weight ,那么我们最好在对应的方向上将width设置为0dp。如果我们使用了非0dp的布局来定义控件大小,那反而有可能达不到我们所想要的效果。

  最后再强调两点:(1)android layout_weight的绘制原理:首先按照控件中声明的尺寸进行分配,然后剩下的尺寸按照weight进行分配,最后全部加到我们相应的控件上去。(2)推荐的使用:如果是在横向的线性布局里布局,不妨先设置android:layout_width="0dp", 然后再去设置layout_weight的值来以此比例调节布局,这应该是规范的做法。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值