关于对Layout_weight的理解

本文详细解析了Android布局文件中的layout_weight属性的作用及计算原理。通过实例演示了当子视图宽度总和小于或大于父视图宽度时,layout_weight如何影响视图的布局。建议在开发实践中合理使用该属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    最近一段时间,有朋友经常问我关于android布局文件里Layout_weight到底是什么作用,之前我也被困扰,随着知识的增长,也慢慢知道了缘由,并将自己的理解分享如下:

    layout_weight的权重不是指父控件的权重,而是指父控件的空间按子控件大小分配完后剩余空间的权重。

    直接上代码上图了

<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"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#800FF8"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black"
            android:text="weight=1"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@android:color/white"
            android:text="weight=2"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@android:color/black"
            android:text="weight=3"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:background="@android:color/white"
            android:text="weight=4"
            android:textColor="@android:color/black" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black"
            android:text="weight=1"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@android:color/white"
            android:text="weight=2"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@android:color/black"
            android:text="weight=3"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:background="@android:color/white"
            android:text="weight=4"
            android:textColor="@android:color/black" />
    </LinearLayout>

</LinearLayout>

运行结果:

    第二行视图中weight=2超出屏幕,weight=3跑偏了,weight=4没了。

    其实android是这样计算的(以布局文件中第一行视图为例):

    首先,android会给每个TextView分配wrap_content宽度,当4个TextView宽度加起来小于屏幕总宽度的话,那一切都会按我们想象的出现4个TextView逐渐增大;反之大于屏幕宽度则会出现布局错位(可以通过以下方法计算错位的位置);

    然后,android将屏幕宽度减去4个TextView总宽度后得到余量(有可能为负),再根据每个TextView的weight分配余量;

    最后,将TextView的宽度加上对应从余量中所分配的大小,就是实际要显示的宽度。

    第二行视图也可通过同样的计算所得,发现Weight=2超过屏幕大小,weight=3跑偏了,weight=4计算为负,显示不出来。

    所以,开发过程中,建议子控件用match_parent,并且为每个子控件分配同样的权重,每个子控件所包含的内容大小尽量一致。



转载于:https://my.oschina.net/nuke1213/blog/167840

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值