LinearLayout中的layout_weight属性深入研究

线性布局是Android中最为常见的一种布局,以前学习四大布局的时候没有深入理解过线性布局中的layout_weight,后知后觉才发现,its amazing,这个属性真是太神奇太好玩了,下面我会深入浅出地对这个布局进行详细的介绍,首先看一下代码布局,横向布局了三个TextView并对其底色进行了设置加以区分:

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

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView1"
        android:background="#DC143C"/>
    
    <TextView 
        android:id="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView2"
        android:background="#0000FF"/>
    
    <TextView 
        android:id="@+id/TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView3"
        android:background="#99FF00"/>

</LinearLayout>


布局非常简单,并没有设置layout_weight属性,因此三个TextView按照横向一次排列开来,大家对于这个布局肯定没有疑问,然后我给第一个控件添加android:layout_weight="1"属性,先介绍一下layout_weight属性,它是用来分配属于空间的一个属性,你可以设置他的权重”,这是官方解释,其实并没有解释清楚,他的真正意义是根据权重分配剩余空间大小。当我把第一个控件的权重设置为1,其他控件不设置时那么默认就是0,因此程序会自动将剩余空间的所有部分分配给控件1,于是我们看到了如下效果:


很容易理解对吧,剩余所有空间的部分都分配给了控件1,下面我们看一个神奇的现象,先将控件1设置android:layout_width="match_parent",同时去掉权重android:layout_weight="1"属性,此时现象如下:


大家都没有疑问吧,控件1将屏幕沾满了,其他两个控件就不会显示在屏幕上了,但是此时我们将android:layout_weight="1"再次设置给控件1,此时就会发现:


为什么?心中的疑问出现了,我们来进行一个简单的数学题,设屏幕宽度为W,TextView2和TextView3宽度都为w0,那么当TextView1宽度属性设置为match_parent时他的宽度也为W,这样程序在执行layout_weight权重分配之前,剩余空间大小是多大呢?

W-W-2*w0=-2*w0

是的你没有看错出现了负数,但反过来一想确实合情合理啊,因为长度超过了屏幕,所以控件被设置在了屏幕之外,不就是负数吗?然后layout_weight属性就要开始起作用了,剩余空间大小是-2*w0,会被分配给TextView1,于是真正占用的空间大小为:W+(-2*w0)=W-2*w0,可怜的TextView1本来可以独占整个屏幕,却被分配了负增长的空间大小,真是物极必反啊。

这样大家就能理解为什么会出现这种情况了吧,再深入一点,你可以把TextView1的宽度随意设置:0dp到1000dp都行然后你会发现他的长度根本不变!!!这又是个简单的数学题:设TextView1的宽度为x,剩余空间:W-x-2*w0,那么TextView1实际占用空间:W-x-2*w0+x=W-2*w0,结果同上。

下面来进阶版的。代码如下:

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

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView1"
        android:layout_weight="2"
        android:background="#DC143C"/>
    
    <TextView 
        android:id="@+id/TextView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView2"
        android:layout_weight="1"
        android:background="#0000FF"/>
    
    <TextView 
        android:id="@+id/TextView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView3"
        android:layout_weight="1"
        android:background="#99FF00"/>

</LinearLayout>

还在疑惑为什么TextView1没见的你赶紧拿起笔算一下吧,textView1实际分配的空间为:W+(2/(2+1+1))*(W-3*W)=0, textView2实际分配的空间为:W+(1/(2+1+1))*(W-3*W)=0.5*W, textView3实际分配的空间为:W+(1/(2+1+1))*(W-3*W)=0.5*W,一目了然。

那如果我想把这几个控件真正的按照weight权重分配空间该怎么做呢?很简单,layout_width=“0”即可,原因不用解释了吧,只要动手算一下就行啦:

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

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView1"
        android:layout_weight="2"
        android:background="#DC143C"/>
    
    <TextView 
        android:id="@+id/TextView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView2"
        android:layout_weight="1"
        android:background="#0000FF"/>
    
    <TextView 
        android:id="@+id/TextView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView3"
        android:layout_weight="1"
        android:background="#99FF00"/>

</LinearLayout>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值