Android中layout_weight属性设置规则

本文详细解析了Android中LinearLayout布局的weight属性工作原理。通过不同示例,展示了如何利用weight属性实现控件间的灵活布局分配,包括按比例分配宽度等场景。

之前简单了解过weight属性原理,但是好长时间没用,今天突然被问起,竟一时想不出来怎么回事了,回来总结下,也算是一个备忘了。

首先了解下weight属性的意义:(这里只考虑宽)规定本控件可继续获得多少父布局所剩宽(L)。先看例子吧

要实现这样的效果:左边文字wrap_content,右边文字wrap_content,中间输入框获取父布局剩下的所有宽度,代码:

    <LinearLayout
        android:id="@+id/holder"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="#ff0099"
            android:gravity="center_vertical"
            android:text="请输入金额:"
            android:textSize="15sp" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#f2f2f2"
            android:hint="这里可输入" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="#44d4ff"
            android:gravity="center_vertical"
            android:padding="5dp"
            android:text="元"
            android:textSize="15sp" />
    </LinearLayout>


父布局宽度设为:L,默认权重weight = 0

每个控件的宽度设定过程可以理解为:

1,先按照每个控件的layout_width的值来设定控件宽度,这里都是wrap_content ,这时都能显示全本控件里面的内容

2,计算剩余宽度 S = L-W1-E-W2;(W1:左边TextView的宽度,E:EditText的宽度,W2:右边TextView的宽度)

3,把剩余的宽度 S 按照weight值进行分配,由于两个TextView的weight = 0,所以EditText能分配到的宽度为 S * 1 /( 0+1+0)=S,所以EditText能获得所有的剩余宽度。


要想这三个控件平分父布局的宽度的话,可以这样设置;


    <LinearLayout
        android:id="@+id/holder"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:background="#ff0099"
            android:gravity="center_vertical"
            android:layout_weight="1"
            android:text="请输入金额:"
            android:textSize="15sp" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#f2f2f2"
            android:hint="这里可输入" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="#44d4ff"
            android:gravity="center_vertical"
            android:padding="5dp"
            android:text="元"
            android:textSize="15sp" />
    </LinearLayout>
把每个控件的宽度都设置为0dp,这样能保证剩余宽度就是总宽度,再按照weight 分就能保证平分


最后分析下这样的效果和对应的代码:

    <LinearLayout
        android:id="@+id/holder"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#ff0099"
            android:gravity="center_vertical"
            android:layout_weight="1"
            android:text="请输入金额:"
            android:textSize="15sp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="2"
            android:background="#f2f2f2"
            android:hint="这里可输入" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="2"
            android:background="#44d4ff"
            android:gravity="center_vertical"
            android:padding="5dp"
            android:text="元"
            android:textSize="15sp" />
    </LinearLayout>

1,按照控件的width属性放置控件,每个控件的宽度都是 L,后两个控件是被挤出屏幕的

2,计算父布局剩余宽度 S = L-(L+L+L)= -2L

3,给每个控件分配剩余宽度,计算后左边TextView宽度 = L+(-2L*1/(1+2+2)) = 3L/5
中间EditText宽度 = L+(-2L*2/(1+2+2))=1L/5

同理右边TextView的宽度也是1L/5,如上图所示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值