android: baselineAligned属性认识及用途

在项目中会经常使用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:background="@android:color/white"
    tools:context=".MainActivity">

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

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

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

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

这里写图片描述

可以看到4个button各占用了LinearLayout宽度的1/4,而且是上边缘和下边缘都是对齐的,是我们预期想要的效果,但是当第三个button的Text内容变长时,会变成如下的效果:

<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:background="@android:color/white"
    tools:context=".MainActivity">

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

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Reload Content"
        android:textSize="12dp" />

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

这里写图片描述

会发现第三个内容由于长度超过LinearLayout宽度的1/4,会换行,这个是正常的,但是却发现整个button位置下移了,没有和其他三个button对齐,很丑,这是怎么回事呢,仔细看button中text会发现Next和第三个button中text第一行是位于一条水平线上的,这个水平线其实就是所谓的baseline,查看Android develper的LinearLayout的API会发现有一个属性:android:baselineAligned,这个属性默认是true,所以LinearLayout里面的子View默认是baseline 对齐的,这就会导致第三个button第一行为了和其他button的text对齐下移
这里写图片描述
这里写图片描述

为了能使四个button对齐,就将android:baselineAligne设置为false。

<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:baselineAligned="false"
    android:background="@android:color/white"
    tools:context=".MainActivity">

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

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Reload Content"
        android:textSize="12dp" />

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

这样可以看到四个button上边缘和下边缘都是对齐的了。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值