Android:layout_weight属性的两种用法

weight属性用于设置控件长和宽所占的比例
首先看一个例子:
Example.1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>


以上两个button控件的长和宽都是wrap,即自动适应;
同时设置weight为1,而其共同的母框架排列方式orientation=”horizontal”,即内部控件水平排列
那么这个时候的效果就是两个控件平分母框架的宽度,同时也是母框架的排列方向上的控件,此时若修改Linearlayout中orientation=”vertical”则会变成:
这里写图片描述
也就是两个控件平分了纵向的空间
拓展
修改weight分别为1,2:
这里写图片描述
可以看到button2的宽度明显没有达到button1的3倍
这是因为button的宽度为wrap,无论如何分配都会是button内容完全显示出来,而将其所在母框架所有空间至少全部显示以后剩余的宽度才会按照weight的比例进行分配,所以button2的宽度并未达到button1的3倍;
而如果修改button1和button2的width=”0dp”,则才会完全按照比例进行分配,如图:
这里写图片描述
此时我们也可以看到button2的宽度基本达到button1的3倍?
所以若需要使用weight按比例分配空间,请将要分配的方向长度设为“0dp”,而这也是google官方推荐的用法。

Example.2
来看第二个例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1" />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Button2" />
</LinearLayout>

这里将宽度设置为match,比例仍然是1:3,,效果如图:
这里写图片描述
可以看到,其显示比例反而变成了3:1,这就是weight的另外一个用法,那么这种用法既然与Example1中的效果相同,那又有什么意义呢,暂时我也不得而知,但是我在实际开发中遇到了这个方法的应用场景,代码如下:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:layout_margin="10dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"/>
        <Button
            android:layout_margin="10dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"/>
        <Button
            android:layout_margin="10dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:layout_margin="10dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="1"/>
        <Button
            android:layout_margin="10dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"/>
    </LinearLayout>

一般为了美观,我们会为各个控件之间增加间距,即通过margin属性来设置,在这个例子中,为每个button都设置了四个方向为10dp的间隙,然后我们将上方3个button平均分配,下面两个控件按照2:1的比例进行分配,
这里写图片描述
明显可以看出,button4的右边缘并未与button2的右边缘对其,同时导致button5喜爱那个做倾斜了好多,画面并不协调,这是为什呢,抱歉我也不知道。
而如果采用上述所讲的第二种用法:
即将button4和button5的宽度改为match_parent,同时设置比例为1:2,效果如图:
这里写图片描述
button4与button2的右边缘完全对齐了,界面是不是更加美观了?

Question

  1. 当为控件设置margin属性的时候,不能完全按照比例进行分配,原因不明;
  2. 通过第二种用法可以修复上述问题,原理不明。

如果你知道我问题的答案,欢迎留言。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值