Android layout_weight 属性解密

在LinearLayout中使用 android:layout_weight,常常不能得到想要的结果,现在详细解密一下。

我们都知道,初始化view的时候会调用如下方法进行height 和 width的设置。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
//widthMeasureSpec,heightMeasureSpec是父类传递的参数,
//结合在XML定义的长宽调整我们的view显示的大小
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
}

在view通过此方法获得他的width和height后,再结合layout_weight属性获得占有剩余的空间的相应大小。剩余空间指的是 屏幕长度 - 所有视图显示需要的长度
话不多说,show the code。

1,设置android:layout_width=”0dp”

<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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:text="11111"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_light"
        />
    <TextView
        android:text="22222"
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="2"
        android:background="@android:color/holo_green_dark" />

    <TextView
        android:text="33333"
        android:id="@+id/text3"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="3"
        android:background="@android:color/holo_red_light" />
</LinearLayout>

android:layout_width=”0dp”,也就是TextView全凭weight的值分配width。
结果如下:
这里写图片描述

我打出的log也可以看出,根据weight的值分配width
屏幕width 1080.
text1 weight 为1,width = 1080 / (1 + 2 + 3) = 180
text2 weight 为2,width = 2 × 1080 / (1 + 2 + 3) = 360
text3 weight 为3,width = 3 × 1080 / (1 + 2 + 3) = 540

 text1 width: 180, height: 90
 text2 width: 360, height: 90
 text3 width: 540, height: 90
  1. 设置android:layout_width=”wrap_content”
<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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:text="11111"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_light"
        />
    <TextView
        android:text="22222"
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="2"
        android:background="@android:color/holo_green_dark" />

    <TextView
        android:text="33333"
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_weight="3"
        android:background="@android:color/holo_red_light" />
</LinearLayout>

TextView获得自己的width后,再根据weight获得最终宽度
结果如下:
这里写图片描述

log如下:

text1 width: 240, height: 90
text2 width: 360, height: 90
text3 width: 480, height: 90

由于我们三个textview的配置属性除了weight外都一致,所以默认分配的width是一致的。我们可以用以下公式求得wrap_content获得宽度。
假设X 为 wrap_content获得的宽度,Y为剩下的宽度
X + Y / 6 = 240
X + Y × 2 / 6 = 360
得出X = 120, Y = 720,也就是每个View自己占比是120,剩余宽度720。
720 + 120 × 3 = 1080,刚好是屏幕的宽度。

3.设置android:layout_width=”match_parent”

<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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:text="11111"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_light"
        />
    <TextView
        android:text="22222"
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_weight="2"
        android:background="@android:color/holo_green_dark" />

    <TextView
        android:text="33333"
        android:id="@+id/text3"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_weight="3"
        android:background="@android:color/holo_red_light" />
</LinearLayout>

结果如下:
这里写图片描述
log如下:

text1 width: 720, height: 90
text2 width: 360, height: 90
text3 width: 0, height: 90

从图中看出text3这个view没有出来,log中显示宽度也为0。这是怎么计算的呢。
可以看一下,每个view的width都是match_parent,那么每个textview的宽度都是1080.要全部显示需要1080 × 3的宽度,但是屏幕只有1080宽。那多出来的坐标就是负值。
我们来计算第一个textview的宽度。
text1自己的宽度为1080,剩余的宽度是 屏幕宽度 - 所有视图的宽度 也就是 1080 - 1080 × 3 = -2160
那text1 的宽度就是 1080 + -2160 / 6 = 1080 - 360 = 720.
text2 的宽度是 1080 + -2160 × 2 / 6 = 360.
text3 的宽度是 1080 + -2160 × 3 / 6 = 0.
可以看出来,和最后显示的结果是一致的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值