线性布局三行三列Android,Android LinearLayout线性布局详解

本文详细介绍了Android中的LinearLayout布局管理器,强调了orientation、gravity和baselineAligned属性的使用,以及如何实现三行三列的布局效果。通过实例展示了不同属性设置对组件排列的影响,并探讨了baselineAlignedChildIndex属性的作用。

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

为了更好地管理android应用的用户界面里的各组件,android提供了布局管理器。通过使用布局管理器,android应用图形用户界面具有良好的平台无关性。推荐使用布局管理器来管理组件的分布、大小,而不是直接设置组件的位置和大小。可以使用布局管理器嵌套布局管理器,即也可作为一个ui组件来使用。

linearlayout可以控制组件横向排列或者纵向排列,内容不会换行,超出屏幕部分将不会显示出来。

学习图解

80325f06fd36025c833038b0ea7d898d.png

linearlayout 常用xml属性及方法

【属性一】orientation 设置子组件的排列方式(单选)

xml:android:orientation="horizontal"

273a355660d5ce7471b8052556a52d10.png

horizontal:横向排列

vertical:纵向排列

java :linearlayout.setorientation(linearlayout.vertical);

linearlayout.horizontal横向排列

eefd20e17a91e838352bcc3f6c3da7e5.png

linearlayout.vertical纵向排列

3531b6a1779b678a3d530ccc60050fee.png

【属性二】gravity设置子组件的对齐方式(多选)

xml:android:gravity="center"

6519a49e955f2844d536eb671d4d4943.png

java :linearlayout.setgravity(gravity.center);

8230d78e4efa579644058c34112207a5.png

【属性三】baselinealigned 设置子元素基准线对弃,默认为true

基准线:

打开的英语练习本,那条红线就是基准线

7141d5896a9f5684f6687290507b80f0.png

9966923671296db7f085840ef091b858.png

xml:android:baselinealigned="false"

5ac6ae17e3a9cb1877e22075c8d03c09.png

java:linearlayout.setbaselinealigned(true);

代码:true

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:baselinealigned="true"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@android:color/holo_red_light"

android:padding="20dp"

android:text="text1"

android:textsize="30sp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@android:color/holo_blue_light"

android:padding="10dp"

android:text="text2"

android:textsize="16sp">

效果:

d2c669806bb13802ab17324d34b79c25.png

32ce53da3547d6f99742e73f0ea99d22.png

【搭配属性三】baselinealignedchildindex linearlayout的基准线以他的第几个子元素为准,下标从0开始

一个linearlayout 里面有很多 textview ,每一个 textview 都有自己的基准线,那么linearlayout可能也是另一个linearlayout的子元素,作为子元素 baselinealignedchildindex 就决定这他的一个基准线

xml:android:baselinealignedchildindex="0"

java:linearlayout.setbaselinealignedchildindex(0);

代码:⭐注意内部的linearlayout,后面将在 第二个linearlayout上添加 baselinealignedchildindex ,搭配  baselinealigned="false" 使用

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:baselinealigned="false"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@android:color/holo_blue_light"

android:text="这是text2"

android:textsize="20sp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@android:color/holo_red_light"

android:text="这是text1"

android:textsize="30sp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@android:color/holo_green_dark"

android:text="这是text2"

android:textsize="15sp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="这是text4"

android:textsize="25sp"

android:background="@android:color/holo_orange_light"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@android:color/black"

android:text="text"

android:textcolor="@android:color/white"

android:textsize="15sp">

效果:

37016fe4160fab6be7a0c81b36be70bd.png

865dcee98c89961ea9cd30aff86057f9.png

e3ef5e20233e2a8d883d7447eaa515f9.png

0d5e00a1c5fbc9ffdaf470a4d66f78ca.png

⭐ 总结

默认linearlayout是没有基准线的,从图一和图三的对比可知。

下标从0开始三个子组件,最大index为2,超过2时布局将不显示

这个属性是用来决定当前linearlayout的基准线时以哪个子组件为准的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值