LinearLayout中的baselineAligned属性

本文通过实例讲解了LinearLayout中baselineAligned属性的作用及其对布局性能的影响。详细解释了当使用权重属性时,该属性如何改变子控件的基线对齐方式。

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

相信在用studio写布局xml的时候LinearLayout会报一些警告

Set android:baselineAligned="false" on this element for better performance less

在使用lint检查时也会出现

Missing baselineAligned attribute

先来看baselineAligned这个属性的字面意思baseline Aligned基线对齐
那和我们平时的开发有什么关系呢
出现此警告时大多(其他还没发现)还使用了权重属性,LinearLayout默认的为true,下面结合一个小例子大家就会很快理解直接上代码

我先设置为false,大家忽略其中的中文

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:baselineAligned="false"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="开始"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="开始"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="开始进行龟兔赛跑比赛了吗"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="开始"
        />
</LinearLayout>
img_7cceb1da0af1a8280a8cec47f3ff6d45.png
Mou icon

设置为true后

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:baselineAligned="true"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="开始"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="开始"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="开始进行龟兔赛跑比赛了吗"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="开始"
        />
</LinearLayout>
img_a9a5c7be728fd634bf6be8c808e06d0f.png
Mou icon
没看到变化?
img_631c139d6737959c8247d0031eb2e83c.png
Mou icon

相信看了这张大家就明白了
设置为true时同时设置了layout_weight属性控件的对齐方式会根据控件内部的内容对齐,当设置为false时会根据控件的上方对齐

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值