Android小技巧——LinearLayout巧妙的平均分配空间

本文介绍了一种在Android开发中实现多个视图平均分配父容器空间的有效方法。通过设置LinearLayout的子视图的layout_weight属性并将其宽度设为0dp,可以确保无论子视图内容如何变化,都能均匀占据父容器的空间。

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

当我们编写Android UI的时候,肯定会遇到这样的UI设计,在屏幕宽度里面线性横向排列有三个View,每个View平分屏幕宽度。乍一看,这个很简单嘛,给这三个View都设置一个相同的width就好嘛,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@android:color/white"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
        android:text="AAAAAAAA"
        android:layout_width="100dp"
        android:layout_height="wrap_content"/>

    <Button
        android:text="BBBBBBBB"
        android:layout_width="100dp"
        android:layout_height="wrap_content"/>

    <Button
        android:text="CCCC"
        android:layout_width="100dp"
        android:layout_height="wrap_content"/>
</LinearLayout>

但问题是它们的宽度是不确定的,有可能动态的变化,比如说它的父布局可能不是屏幕宽度等等。所以这个方法是下下策。也许有人会说,这个也不难吗,我在java代码里面动态的去设置它们的宽度相同就好了。但这个的前提是需要先获取它的父布局里的宽度,而且当当activity生命周期执行到onresume的时候,整个当前页面UI其实还没有真正的绘制到window中,这也是我们经常在生命周期中获取到某个view的width、height为0的原因,考虑到这个,开发者往往还要借助View.getViewTreeObserver().addOnGlobalLayoutListener的布局监听来解决,既然add了,那就有remove操作等,然而回到我们的问题,我不过是要view平均分配空间而已,真的需要添加如此多的代码?

另外有一些人会说,我使用LiearLayout,设置三个view的layout_weight属性都相同不就可以了吗?例如下面代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@android:color/white"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
        android:layout_weight="1"
        android:text="aaa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="bbb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="ccc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

这里写图片描述

看上去,上面的方法好像可以解决问题,然而如果每个子view的内容宽度不一样的时候也能保持平均分配吗?答案当然是NO!例如下面代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@android:color/white"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
        android:layout_weight="1"
        android:text="aaaaaaaaaaaaaaa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="bbbbb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_weight="1"
        android:text="c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

这里写图片描述

所以通过设置相同的layout_weight只能解决每个子view内容宽度都相同的情况下的问题。

现在说一个非常实用的小技巧:
在LinearLayout布局中,巧妙的设置width/height和layout_weight属性就能达到子view平均分配父view空间的效果
例子代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@android:color/white"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
        android:layout_weight="1"
        android:text="aaaaaaaaaaaa"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

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

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

这里写图片描述

必须要注意的是:务必将width和layout_weight搭配使用,否则代码会报错

好了,上面说的是平分父view width的例子,平分父view height的方式也是一样的,此处就不赘述了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值