LinearLayout的weight(权重属性)

weight(权重)属性详解:

①最简单用法:

如图:

实现代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="#ADFF2F"
        android:text="weight为1"
        android:layout_weight="1"/>


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="#DA70D6"
        android:text="weight为2"
        android:layout_weight="2"/>

</LinearLayout>

要实现第一个的1:1的效果,只需要分别把两个LinearLayout的weight改成1和1就可以了 用法归纳: 按比例划分竖直方向:将涉及到的View的android:width属性设置为0dp,然后设置为android weight属性设置比例即可;类推,水平方向,只需设android:height为0dp,然后设weight属性即可! 大家可以自己写个水平方向的等比例划分的体验下简单用法!

②weight属性详解:

当然,如果我们不适用上述那种设置为0dp的方式,直接用wrap_content和match_parent的话, 则要接着解析weight属性了,分为两种情况,wrap_content与match_parent!另外还要看 LinearLayout的orientation是水平还是竖直,这个决定哪个方向等比例划分

1)wrap_content比较简单,直接就按比例的了


实现代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ADFF2F"
        android:text="weight为1"
        android:layout_weight="1"/>


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#DA70D6"
        android:text="weight为2"
        android:layout_weight="2"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#00ff66"
        android:text="weight为3"
        android:layout_weight="3"/>
</LinearLayout>

2)match_parent(fill_parent):这个则需要计算了

我们写这段简单的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/LinearLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent" >    
    
    <TextView    
        android:layout_weight="1"    
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:text="one"     
        android:background="#98FB98"    
     />    
     <TextView    
        android:layout_weight="2"    
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:text="two"     
        android:background="#FFFF00"    
     />    
     <TextView    
        android:layout_weight="3"    
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:text="three"     
        android:background="#FF00FF"    
     />    
    
</LinearLayout> 
 

运行效果图:


看到上面的现象,很奇怪是吧,,这比例是2:1,那么three去哪了?代码里面明明有 three的啊,还设置了3,12的比例也不对啊,1:2:3却变成了2:1:0,怎么会这样呢

:这里其实没那么简单的,根据找到的方法比较容易理解的一种

step 1:个个都是fill_parent(match_parent),但是屏幕只有一个,那么1 - 3 = - 2 fill_parent 

step 2:依次比例是1/6,2/6,3/6 

step 3:先到先得,先分给one,计算: 1 - 2 * (1/6) = 2/3 fill_parent 接着到two,计算: 1 - 2 * (2/6) = 1/3 fill_parent 最后到three,计算 1 - 2 * (3/6) = 0 fill_parent 

step 4:所以最后的结果是:one占了两份,two占了一份,three什么都木有 以上就是为什么three没有出现的原因了,或许大家看完还是有点蒙,没事,我们举多几个例子试试就知道了!

比例为:1:1:1


按照上面的计算方法算一次,结果是:1/3 1/3 1/3,没错

接着我们再试下:2:3:4


计算结果:5/9 3/9 1/9,对比效果图,5:3:1,也没错,所以这个计算方法你可得mark下了!

③Java代码中设置weight属性:

setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,     
        LayoutParams.WRAP_CONTENT, 1)); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值