Android开发学习笔记之详解五大布局

简介: Android开发学习笔记之详解五大布局

为了适应各式各样的界面风格,Android系统提供了5种布局,这5种布局分别是:


LinearLayout(线性布局)


TableLayout(表格布局)


RelativeLayout(相对布局)


AbsoluteLayout(绝对布局)


FrameLayout(框架布局)


利用这五种布局,可以在屏幕上将控件随心所欲的摆放,而且控件的大小和位置会随着屏幕大小的变化作出相应的调整。下面是这五个布局在View的继承体系中的关系:

 



一,LinearLayout(线性布局)


   在一个方向上(垂直或水平)对齐所有子元素

一个垂直列表每行将只有一个子元素(无论它们有多宽)

一个水平列表只是一列的高度(最高子元素的高度来填充)


下面是一个简单的线性布局的例子:


01 <?xml version="1.0" encoding="utf-8"?> 
02 <LinearLayout 
03 xmlns:android="http://schemas.android.com/apk/res/android" 
04 android:layout_width="match_parent" 
05 android:layout_height="match_parent" android:orientation="vertical"> 
06 <EditText android:text="EditText" 
07   
08 android:id="@+id/editText1" 
09   
10 android:layout_height="wrap_content" 
11   
12 android:layout_width="fill_parent"> 
13   
14 </EditText> 
15 <LinearLayout android:id="@+id/linearLayout1" 
16   
17 android:layout_height="fill_parent" 
18   
19 android:layout_width="fill_parent" 
20   
21 android:gravity="right"> 
22 <Button android:id="@+id/button2" 
23   
24 android:text="Button" 
25   
26 android:layout_width="wrap_content" 
27   
28 android:layout_height="wrap_content"></Button> 
29 <Button android:text="Button" 
30   
31 android:id="@+id/button1" 
32   
33 android:layout_width="wrap_content" 
34   
35 android:layout_height="wrap_content"></Button> 
36 </LinearLayout> 
37 </LinearLayout>

复制代码

最外层布局为垂直线性布局,宽度为整个屏幕(fill_parent),高度为刚好适合子控件(wrap_content)。然后依次添加一个EditText,一个水平布局的LinearLayout,在这个线性布局里面,摆放两个Button,该线性布局的gravity属性设置为”right”,所以里面的两个Button靠右显示。


二,TableLayout(表格布局)


把子元素放入到行与列中

不显示行、列或是单元格边界线

单元格不能横跨行,如HTML中一样

表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。TableRow可以添加子控件,每添加一个为一列。


android:layout_colum官方解释:The index of the column in which this child should be,也即是设置该控件在TableRow中所处的列。

android:layout_span官方解释:Defines how many columns this child should span,也即是设置该控件所跨越的列数。


android:collapseColumns官方解释:The 0 based index of the columns to collapse. The column indices must be separated by a comma: 1, 2, 5.也即是将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。


android:stretchColumns官方解释:The 0 based index of the columns to stretch. The column indices must be separated by a comma: 1, 2, 5. You can stretch all columns by using the value “*” instead. Note that a column can be marked stretchable and shrinkable at the same time.也即是设置指定的列为可伸展的列,可伸展的列会尽量伸展以填满所有可用的空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。


android:shrinkColumns官方解释:The 0 based index of the columns to shrink. The column indices must be separated by a comma: 1, 2, 5. You can shrink all columns by using the value “*” instead. 设置指定的列为可收缩的列。当可收缩的列太宽以至于让其他列显示不全时,会纵向延伸空间。当需要设置多列为可收缩时,将列序号用逗号隔开。


下面用一个例子简单说明TableLayout的用法:

01 <?xml version="1.0" encoding="utf-8"?> 
02 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
03 android:layout_width="fill_parent" 
04 android:layout_height="fill_parent" 
05 android:stretchColumns="1"> 
06 <TableRow> 
07 <TextView 
08 android:layout_column="1" 
09 android:padding="3dip" android:text="Row1"/> 
10 <TextView 
11 android:text="1" 
12 android:gravity="right" 
13 android:padding="3dip" /> 
14 </TableRow> 
15 <View 
16 android:layout_height="2dip" 
17 android:background="#FF909090" /> 
18 <TableRow> 
19 <TextView 
20 android:text="*" 
21 android:padding="3dip" /> 
22 <TextView 
23 android:text="Row12" 
24 android:padding="3dip" /> 
25 <TextView 
26 android:text="2" 
27 android:gravity="right" 
28 android:padding="3dip" /> 
29 </TableRow> 
30 <View 
31 android:layout_height="2dip" 
32 android:background="#FF909090" /> 
33 <TableRow> 
34 <TextView 
35 android:layout_column="1" 
36 android:text="Row13" 
37 android:padding="3dip" /> 
38 </TableRow> 
39 </TableLayout>

复制代码

三、RelativeLayout(相对布局)


相对布局的子控件会根据它们所设置的参照控件和参数进行相对布局。参照控件可以是父控件,也可以是其它子控件,但是被参照的控件必须要在参照它的控件之前定义。下面是一个简单的例子:

01 <?xml version="1.0" encoding="utf-8"?> 
02 <?xml version="1.0" encoding="utf-8"?> 
03 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
04 android:layout_width="fill_parent" 
05 android:layout_height="fill_parent" 
06 > 
07 <AnalogClock 
08 android:id="@+id/aclock" 
09 android:layout_width="wrap_content" 
10 android:layout_height="wrap_content" 
11 android:layout_centerInParent="true" /> 
12 <DigitalClock 
13 android:id="@+id/dclock" 
14 android:layout_width="wrap_content" 
15 android:layout_height="wrap_content" 
16 android:layout_below="@id/aclock" 
17 android:layout_alignLeft="@id/aclock" 
18 android:layout_marginLeft="40px" /> 
19 <TextView 
20 android:layout_width="wrap_content" 
21 android:layout_height="wrap_content" 
22 android:text="当前时间:" 
23 android:layout_toLeftOf="@id/dclock" 
24 android:layout_alignTop="@id/aclock"/> 
25 </RelativeLayout>

复制代码

四、AbsoluteLayout(绝对布局)


绝对布局的子控件需要指定相对于此坐标布局的横纵坐标值,否则将会像框架布局那样被排在左上角。手机应用需要适应不同的屏幕大小,而这种布局模型不能自适应屏幕尺寸大小,所以应用的相对较少。下面以一个例子简单说明绝对布局:

01 <?xml version="1.0" encoding="utf-8"?> 
02 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
03 android:layout_width="fill_parent" 
04 android:layout_height="fill_parent" 
05 > 
06 <TextView 
07 android:layout_width="wrap_content" 
08 android:layout_height="wrap_content" 
09 android:layout_x="10px" 
10 android:layout_y="10px" android:text="Textview"/> 
11 <TextView 
12 android:layout_width="wrap_content" 
13 android:layout_height="wrap_content" 
14 android:layout_x="30px" 
15 android:layout_y="30px" android:text="Textview"/> 
16 <TextView 
17 android:layout_width="wrap_content" 
18 android:layout_height="wrap_content" 
19 android:layout_x="50px" 
20 android:layout_y="50px" android:text="Textview"/> 
21 </AbsoluteLayout>

复制代码

五、FrameLayout(框架布局)



框架布局是最简单的布局形式。所有添加到这个布局中的视图都以层叠的方式显示。第一个添加的控件被放在最底层,最后一个添加到框架布局中的视图显示在最顶层,上一层的控件会覆盖下一层的控件。这种显示方式有些类似于堆栈。下面举一个简单的例子

 

01 <?xml version="1.0" encoding="utf-8"?> 
02 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
03 android:layout_width="fill_parent" android:layout_height="fill_parent"> 
04 <LinearLayout android:id="@+id/linearLayout1" 
05 android:layout_height="match_parent" 
06 android:layout_width="match_parent"> 
07 <Button android:text="Button" 
08 android:id="@+id/button1" 
09 android:layout_width="wrap_content" 
10 android:layout_height="wrap_content"></Button> 
11 </LinearLayout> 
12 <LinearLayout android:layout_width="match_parent" 
13 android:id="@+id/linearLayout3" 
14 android:layout_height="match_parent" 
15 android:gravity="bottom|right"> 
16 <Button android:text="Button" 
17 android:id="@+id/button3" 
18 android:layout_width="wrap_content" 
19 android:layout_height="wrap_content"></Button> 
20 </LinearLayout> 
21 <LinearLayout android:layout_height="match_parent" 
22 android:id="@+id/linearLayout2" 
23 android:layout_width="match_parent" 
24 android:gravity="right"> 
25 <Button android:text="Button" 
26 android:id="@+id/button2" 
27 android:layout_width="wrap_content" 
28 android:layout_height="wrap_content"></Button> 
29 </LinearLayout> 
30 <LinearLayout android:layout_width="match_parent" 
31 android:id="@+id/LinearLayout01" 
32 android:layout_height="match_parent" 
33 android:gravity="bottom|left"> 
34 <Button android:id="@+id/Button01" 
35 android:text="Button" 
36 android:layout_width="wrap_content" 
37 android:layout_height="wrap_content"></Button> 
38 </LinearLayout> 
39 </FrameLayout>

复制代码

下面介绍一下RelativeLayout用到的一些重要的属性:


第一类:属性值为true或false

android:layout_centerHrizontal                                           水平居中

android:layout_centerVertical                                            垂直居中

android:layout_centerInparent                                           相对于父元素完全居中

android:layout_alignParentBottom                     贴紧父元素的下边缘

android:layout_alignParentLeft                        贴紧父元素的左边缘

android:layout_alignParentRight                       贴紧父元素的右边缘

android:layout_alignParentTop                        贴紧父元素的上边缘

android:layout_alignWithParentIfMissing               如果对应的兄弟元素找不到的话就以父元素做参照物


第二类:属性值必须为id的引用名“@id/id-name”

android:layout_below                          在某元素的下方

android:layout_above                          在某元素的的上方

android:layout_toLeftOf                       在某元素的左边

android:layout_toRightOf                     在某元素的右边


android:layout_alignTop                      本元素的上边缘和某元素的的上边缘对齐

android:layout_alignLeft                      本元素的左边缘和某元素的的左边缘对齐

android:layout_alignBottom                 本元素的下边缘和某元素的的下边缘对齐

android:layout_alignRight                    本元素的右边缘和某元素的的右边缘对齐


第三类:属性值为具体的像素值,如30dip,40px

android:layout_marginBottom              离某元素底边缘的距离

android:layout_marginLeft                   离某元素左边缘的距离

android:layout_marginRight                 离某元素右边缘的距离

android:layout_marginTop                   离某元素上边缘的距离

相关文章
|
6天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
29天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
14 0
|
3天前
|
数据库 Android开发 开发者
安卓应用开发:构建高效用户界面的策略
【4月更文挑战第24天】 在竞争激烈的移动应用市场中,一个流畅且响应迅速的用户界面(UI)是吸引和保留用户的关键。针对安卓平台,开发者面临着多样化的设备和系统版本,这增加了构建高效UI的复杂性。本文将深入分析安卓平台上构建高效用户界面的最佳实践,包括布局优化、资源管理和绘制性能的考量,旨在为开发者提供实用的技术指南,帮助他们创建更流畅的用户体验。
|
20天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
22天前
|
监控 算法 Android开发
安卓应用开发:打造高效启动流程
【4月更文挑战第5天】 在移动应用的世界中,用户的第一印象至关重要。特别是对于安卓应用而言,启动时间是用户体验的关键指标之一。本文将深入探讨如何优化安卓应用的启动流程,从而减少启动时间,提升用户满意度。我们将从分析应用启动流程的各个阶段入手,提出一系列实用的技术策略,包括代码层面的优化、资源加载的管理以及异步初始化等,帮助开发者构建快速响应的安卓应用。
|
22天前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
25 1
Android开发之使用OpenGL实现翻书动画
|
22天前
|
Android开发 开发者
Android开发之OpenGL的画笔工具GL10
这篇文章简述了OpenGL通过GL10进行三维图形绘制,强调颜色取值范围为0.0到1.0,背景和画笔颜色设置方法;介绍了三维坐标系及与之相关的旋转、平移和缩放操作;最后探讨了坐标矩阵变换,包括设置绘图区域、调整镜头参数和改变观测方位。示例代码展示了如何使用这些方法创建简单的三维立方体。
19 1
Android开发之OpenGL的画笔工具GL10
|
29天前
|
Android开发
Android开发小技巧:怎样在 textview 前面加上一个小图标。
Android开发小技巧:怎样在 textview 前面加上一个小图标。
12 0
|
29天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
30天前
|
缓存 Java Android开发
安卓应用开发中的内存优化策略
在移动应用开发领域,性能一直是衡量应用质量的重要指标之一。特别是对于安卓平台,由于设备的硬件配置多样化,内存管理成为开发者面临的重大挑战。本文将深入探讨针对安卓平台的内存优化技巧,包括内存泄漏的预防、合理使用数据结构和算法、以及高效的资源释放机制。通过这些方法,开发者可以显著提升应用的性能和用户体验。