Android ApiDemos示例解析(134):Views->Layouts->Baseline->2. Bottom

本文深入解析LinearLayout布局中android:layout_gravity属性的用法,包括其不同参数的含义与效果,通过实例展示如何实现子View的对齐方式调整,特别强调了在设置bottom对齐方式时,由于LinearLayout默认的baseline对齐导致的文本基准线对齐问题。

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

LinearLayout的android:layout_gravity 定义了子View的对齐方式可以有下面几种:

  • top: 和父容器的顶端对齐,不对子View进行缩放。
  • bottom: 和父容器的底端对齐,不对子View进行缩放。
  • left: 和父容器的左端对齐,不对子View进行缩放。
  • right: 和父容器的右端对齐,不对子View进行缩放。
  • center_vertical: 将子View居中(垂直居中),不对子View进行缩放。
  • fill_vertical: 将子View的垂直方向拉伸充满容器。
  • center_horizontal: 将子View居中(水平居中),不对子View进行缩放。
  • fill_horizontal: 将子View的水平方向拉伸充满容器。
  • center: 将子View居中(水平和垂直都居中),不对子View进行缩放。
  • fill: 将子View水平和垂直方向都拉伸充满整个容器。
  • clip_vertical:垂直裁剪子View
  • clip_horizontal: 水平裁剪子View。

多种选项可以使用“|” 隔开。

本例对三个子View都使用bottom 对齐方式,应为Linerlayout缺省采用baseline 对齐,因此三个View文字基准线是对齐的。

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginRight=”3dip”
android:layout_gravity=”bottom”
android:text=”@string/baseline_2_label” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginRight=”3dip”
android:layout_gravity=”bottom”
android:text=”@string/baseline_2_button” />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”bottom”
android:textSize=”20sp”
android:text=”@string/baseline_2_bigger” />

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值