layout_weight探究

在线性布局中,如果我们想设置三个等比例的textview控件,我们可以通过layout_weight指定该子元素在LinearLayout中所占的权重来实现,我们举个例子,设置三个比例为1:1:1的控件,代码如下:

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical">

   <TextView
       android:text="abc"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:background="#ff0000"/>
<TextView
        android:text="abc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0"/>
<TextView
        android:text="abc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ffa800ff"/>
</LinearLayout>

效果如下,三个textview控件的比例为1:1:1

这里写图片描述

那么,如果我希望设置三个比例为1:2:3的textview控件,我们是不是也可用类似的方式呢?

那么我们可以通过修改layout_weight属性值来实验一下

  <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:background="#ff0000"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#ff0"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#ffa800ff"/>

这里写图片描述

结果出人意料,不仅没有出现我们设想的那种1:2:3的效果,反而我们(通过颜色)还发现第三个textview控件不见了,按照权重的比值,应当是权重大的分得的空间最多,但现在反而权重大的分的空间最小,那么为什么会出现这种情况呢?

其实在上面的代码中,我每一个textview控件的layout_heigth和layout_width的属性值都为match_parent,若是改成wrap_content也会有这样的情况吗?
那么我们用控制变量法来实验一下,由于控件是垂直排布,那么现在将layout_width的属性值保持不变,将layout_height的属性值改成wrap_content,看看会是怎样的效果呢?我们来实现一下

<TextView
       android:text="abc"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:background="#ff0000"/>
    <TextView
        android:text="abc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="#ff0"/>
    <TextView
        android:text="abc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:background="#ffa800ff"/>

这里写图片描述
结果正好是1:2:3。

那么wrap_content和match_parent有什么区别呢?

  若属性值为match_parent,则说明该组件与其父类容器有相同高度或宽度;
  若属性值为wrap_content,则说明该组件的宽度取决与它的内容——能包裹它的内容即可;

也就是说,当我们的控件的layout_height和layout_width的属性值都为match_parent时,每一个控件都应该具有与父类容器一样的高度和宽度。

我们先将整个空间的大小设为1,因为每一个控件的大小都是同父控件一样的,则设总的大小为3,那么此时整个屏幕的剩余空间应该为S = 1 - 3 = -2,也就是剩下-2的剩余空间。
那么我们再根据比例来重新计算一下各个控件的占比:

控件实际占比=父空间+剩余空间*控件在剩余空间的占比,即
第一个控件的占比= 1 + (-2) * 1/6 = 2/3
第二个控件的占比= 1 + (-2) * 2/6 = 1/3
第三个控件的占比= 1 + (-2) * 3/6 = 0
实际占比为2:1:0

这也就解释了为什么会出现像上面那样的情况。
若是换一种比例会不会也可以用这种方法来解释呢?
我们将比例设为1:2:1
再根据我们的计算

控件实际占比=父空间+剩余空间*控件在剩余空间的占比,即
第一个控件的占比= 1 + (-2) * 1/4 = 1/2
第二个控件的占比= 1 + (-2) * 2/4 = 0
第三个控件的占比= 1 + (-2) * 3/4 = 1/2
实际占比为1:0:1

这里写图片描述

结果跟我们计算的一样。

总结

1、当控件的高度或者宽度都与父控件相匹配时,即属性值为match_parent时,通过layout_weight设置的权重来实现各个控件在屏幕中占比时,其实际实际占比应该=父空间+控件在剩余空间的比值。
2、应该注意wrap_content和match_parent在这次讨论中的区别在哪。通过前者给控件的高度或宽度赋值,其实际大小只要能包住控件里面的内容就可以了,那么此时通过layout_weight设置的权重,各个控件间的占比就是实际占比;而后者的则不是。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值