[读书笔记]weight知多少

在LinearLayout(线性布局)中有个weight(权重)属性是大家经常用到的,下面有三种weight的使用场景,你能清楚的分辨控件各自所占的百分比吗?

  • 场景一:
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/bt_buttom"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="@string/bt_buttom2"
        />
    </LinearLayout>
  • 场景二:
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/bt_buttom"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="@string/bt_buttom2"
            />
    </LinearLayout>
  • 场景三:
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/bt_buttom"
            />
        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="@string/bt_buttom2"
            />
    </LinearLayout>

在上面的三个场景中我们都给第一个Button设置的weight=1,给第二个Button设置的weight=2,不同的是给他们的宽度设置的不同,第一个场景是都为0dp,第二个场景是 都为match_parent.第三个分别给他们指定了宽度300dp和100dp.那三个场景中两个控件各占屏幕宽度的多少呢?
结果:
这里写图片描述
在你的预料中吗?出现这样的占宽比究竟底层究竟是怎样的算法呢?我们给出下面这样一个计算公式:

控件实际宽度=初始宽度+剩余空间所占百分比宽度

我的虚拟机屏幕尺寸是768x1280

  • 在场景一中:

第一个Button设置的weight=1,给第二个Button设置的weight=2,他们的宽度都是0dp,代入公式中我们来算一下:

  • 剩余空间宽度=768dp-(0dpx2)=768dp

    Button1:
    初始宽度=0dp
    百分比=1/(1+2)=1/3
    Button1宽度=0dp+1/3*768dp=256dp
    Button2:
    初始宽度=0dp
    百分比=2/(1+2)=2/3
    Button2宽度=0dp+2/3*768dp=512dp
    所以控件宽度比是1:2

  • 在场景二中:

第一个Button设置的weight=1,给第二个Button设置的weight=2,他们的宽度都是match_parent,代入公式中我们来算一下:
- 剩余空间宽度=768dp-(768dpx2)=-768dp
Button1:
初始宽度=match_parent=768dp
百分比=1/(1+2)=1/3
Button1宽度=768dp+1/3*-768dp=512dp
Button2:
初始宽度=match_parent=768dp
百分比=2/(1+2)=2/3
Button2宽度=768dp+2/3*-768dp=256dp
所以控件宽度比是2:1

  • 在场景三中:

第一个Button设置的weight=1,给第二个Button设置的weight=2,他们的宽度分别是300dp和100dp,代入公式中我们来算一下:
- 剩余空间宽度=768dp-(300dp+100dp)=368dp
Button1:
初始宽度=300dp
百分比=1/(1+2)=1/3
Button1宽度=300dp+1/3*368dp=422.7dp
Button2:
初始宽度=100dp
百分比=2/(1+2)=2/3
Button2宽度=100dp+2/3*368dp=345.3dp

  • 巧用weight
    有的时候会碰到这样的情况
    这里写图片描述
    我们只要在屏幕放置一个1/2宽度或者1/3宽度的控件怎么办?我们可以用weight和weightSum这两个属性来实现
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:orientation="horizontal">
        <Button
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"/>
    </LinearLayout>

通过指定当前父容器的总的权重来控制当前控件所占比例

其实在布局文件中.我们会看到跟多layout开头的属性,比如layout_width,layout_height,layout_weight,layout_gravity.layout_margin等属性其实他们实际的值都不是自己指定的,而是他们的父容器.我们传入的值只是父容器计算实际分配值的参数.我们来举个例子

举例一:

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/bt_buttom"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/bt_buttom2"
            />
    </LinearLayout>

举例二:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/bt_buttom"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/bt_buttom2"
            />
    </LinearLayout>

在第一个举例中,第一个Button宽度是wrap_content,第二个Button宽度是match_parent,在举例二中他们这两个值恰恰相反:那实际运行回事怎样的效果呢?
这里写图片描述
在第一个举例中,虽然Button2向父容器请求的宽度是match_parent也就是768dp,但是实际并没有这么宽,这里就有个类似先来后到的思想,先处理Button1先请求的宽度再处理Button2请求的宽度.样例二中也一样,因为先处理了Button1 请求的宽度match_parent(768dp),那Button2再怎请求也没有剩余显示空间给它了.

我的博客网站:http://huyuxin.top/欢迎大家访问,评论.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值