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

当我们编写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的方式也是一样的,此处就不赘述了。

  • 10
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Studio中的LinearLayout是一种用于在垂直或水平方向上排列子视图的布局容器。它可以根据指定的权重来分配子视图的空间。 在版本一中,LinearLayout的方向是垂直的,子视图按照顶部、中部和底部的顺序排列,并使用不同的背景颜色进行区分。在版本二中,LinearLayout的方向仍然是垂直的,但是使用了分隔线来分隔子视图,并通过dividerPadding属性设置了分隔线与子视图之间的间距。 如果你想在Android Studio中创建一个LinearLayout,你可以通过XML布局文件来定义它,并在其中添加子视图。你可以指定LinearLayout的方向、布局宽度和高度等属性,还可以设置权重来调整子视图的大小。此外,你还可以使用分隔线来美化LinearLayout的外观。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Android Studio —— LinearLayout](https://blog.csdn.net/automoblie0/article/details/122548137)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Android LinearLayout实现自动换行效果](https://download.csdn.net/download/weixin_38737366/14880886)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值