剩余空间为负值时layout_weight的理解

Android布局权重理解与实践
本文深入探讨了Android中LinearLayout的layout_weight属性的使用。当TextView的width设为wrap_content时,剩余空间按权重比例分配;若设为fill_parent,则在分配了fill_parent宽度后,剩余空间变为负值,导致比例变化。Google推荐使用0dp替代wrap_content以更准确地利用layout_weight进行布局。通过示例代码解释了这一现象,并提供了最佳实践建议。

Alt

1.剩余空间为负值时layout_weight的理解

<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:orientation="horizontal" >
    <TextView
        android:background="#ccc"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_weight="1"
        android:textSize="30sp" />   
    <TextView
        android:background="#aaa"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_weight="2"
        android:textSize="30sp" />
     <TextView
        android:background="#999"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"    
        android:layout_weight="3"
        android:textSize="30sp" />
</LinearLayout>

Alt

当三个 TextView 的宽度都设为 wrap_content 的时候,比例是能够按照 layout_weight 分配的。原因是系统先给3个TextView分配他们的宽度值wrap_content(宽度足以包含他们的内容即可,此时TextView无内容),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个TextView,所以就出现了上面的效果。

2.我们再试试,把 wrap_content 换成 fill_parent 看看(为了方便查看,我们把 layout_weight 分别改为 1 2 2)。效果就成这样了(比例3 1 1):

Alt

为什么呢?
系统先给3个 TextView 分配他们所要的宽度 fill_parent,也就是说每一都是填满他的父控件,这里就是屏幕的宽度。那么这时候的 
剩余空间=1 parent_width - 3 parent_width = -2 parent_width
(parent_width指的是屏幕宽度)
那么第一个 TextView 的实际所占宽度应该是 fill_parent 的宽度,即 parent_width + 他所占剩余空间的权重比列 1/5 * 剩余空间大小(-2 parent_width)=3/5parent_width
同理第二个TextView的实际所占宽度为parent_width + 2/5*(-2parent_width)=1/5parent_width;
第三个TextView的实际所占宽度为parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列显示了。
大概就是这样。
Google 官方推荐,当使用 layout_weight 属性时,可以将 width 设为 0dp,效果跟设成 wrap_content 是一样的。这样 layout_weight 就可以理解为占比了。

3.Google 官方推荐,当使用 layout_weight 属性时,可以将 width 设为 0dp,效果跟设成 wrap_content 是一样的。这样 layout_weight 就可以理解为占比了。

<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:orientation="horizontal" >
    <TextView
        android:background="#ccc"   
        android:layout_width="0dp"   
        android:layout_height="wrap_content"   
        android:layout_weight="1"
        android:textSize="30sp" />   
    <TextView
        android:background="#aaa"   
        android:layout_width="0dp"   
        android:layout_height="wrap_content"   
        android:layout_weight="2"
        android:textSize="30sp" />
     <TextView
        android:background="#999"   
        android:layout_width="0dp"   
        android:layout_height="wrap_content"    
        android:layout_weight="3"
        android:textSize="30sp" />
</LinearLayout>

Alt

文章转自: 现代魔法学院

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值