layout_weight 和 weightSum

20 篇文章 0 订阅

LinearLayout的layout_weight给适配带来了很多方便, 但有些新司机不是很了解父布局中还可以设置一个weightSum属性

<?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="wrap_content"
    android:background="@color/colorAccent"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:weightSum="2">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Weight" />

</LinearLayout>

这里写图片描述

这里写图片描述

如果LinearLayout 中没有android:weightSum=”2”这句
Button宽度将充满整个LinearLayout, 有了这个后, 无论横竖屏, 都刚好是父布局宽度的一半.

也就是, 如果不指定父布局的android:weightSum, 则以所有子视图的 layout_weight 属性的累加值作为总和的最大值。如果指定了, 则总的就是我们指定的值.
如上面, 指定了2, 子控件是1, 那么子控件为父容器的一半.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值