Android 布局简要范例
Android的布局决定着实际的UI界面呈现情况,正是这些UI界面的组合与千变万化,才呈现出了各式各样的风格。
而这些基础的布局框架结构很重要,需要玩的很熟悉。我将以前参考的部分代码示例,所做的相关实践在这里记录一下,稍后相关代码也会删除掉,这里做个备忘。
好了,现在开始:
我的代码结构是这样子的:
每个布局文件个类,如果要做具体的个性化,在其中的包里面扩展;
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦。组件按照布局的要求依次排列,就组成了用户所看见的界面。Android的五大布局分别是LinearLayout(线性布局)、FrameLayout(单帧布局)、RelativeLayout(相对布局)、AbsoluteLayout(绝对布局)和TableLayout(表格布局)。
LinearLayout:
LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。
LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。android:layout_weight遵循数值越小,重要度越高的原则。显示效果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "match_parent" > <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:background= "#ff000000" android:text= "@string/hello" /> <LinearLayout android:orientation= "horizontal" android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:background= "#ff654321" android:layout_weight= "1" android:text= "1" /> <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:background= "#fffedcba" android:layout_weight= "2" android:text= "2" /> </LinearLayout> <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text="LinearLayout:LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。 LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。加入一行只有一个文本框,那么它的默认值就为 0 ,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为 1 。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为 1 和 2 ,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。android:layout_weight遵循数值越小,重要度越高的原则。显示效果如下: "/> </LinearLayout> |
FrameLayout:
FrameLayout是五大布局中最简单的一个布局, 在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的 子元素之上,将前面的子元素部分和全部遮挡。显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二 个TextView的部分位置。
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff0000" android:gravity="center" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00ff00" android:gravity="center" android:text="2"/> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#0000ff" android:gravity="center" android:text="3"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00ffff" android:gravity="center" android:text=" FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二个TextView的部分位置。"/> </FrameLayout>
AbsoluteLayout:
AbsoluteLayout是绝对位置布局。在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。在此布局中的子元素可以相互重叠。在实际开发中,通常不采用此布局格式,因为它的界面代码过于刚性,以至于有可能不能很好的适配各种终端。显示效果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version= "1.0" encoding= "utf-8" ?> <AbsoluteLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <TextView android:layout_width= "50dp" android:layout_height= "50dp" android:background= "#ffffffff" android:gravity= "center" android:layout_x= "50dp" android:layout_y= "50dp" android:text= "1" /> <TextView android:layout_width= "50dp" android:layout_height= "50dp" android:background= "#ff654321" android:gravity= "center" android:layout_x= "25dp" android:layout_y= "25dp" android:text= "2" /> <TextView android:layout_width= "50dp" android:layout_height= "50dp" android:background= "#fffedcba" android:gravity= "center" android:layout_x= "125dp" android:layout_y= "125dp" android:text= "3" /> <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_x= "5dp" android:layout_y= "200dp" android:text= "AbsoluteLayout是绝对位置布局。在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。在此布局中的子元素可以相互重叠。在实际开发中,通常不采用此布局格式,因为它的界面代码过于刚性,以至于有可能不能很好的适配各种终端。显示效果如上" /> </AbsoluteLayout> |
RelativeLayout:
RelativeLayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below, android:layout_above等。子元素就通过这些属性和各自的ID配合指定位置关系。注意在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。
RelativeLayout里常用的位置属性如下:
android:layout_toLeftOf —— 该组件位于引用组件的左方
android:layout_toRightOf —— 该组件位于引用组件的右方
android:layout_above —— 该组件位于引用组件的上方
android:layout_below —— 该组件位于引用组件的下方
android:layout_alignParentLeft —— 该组件是否对齐父组件的左端
android:layout_alignParentRight —— 该组件是否齐其父组件的右端
android:layout_alignParentTop —— 该组件是否对齐父组件的顶部
android:layout_alignParentBottom —— 该组件是否对齐父组件的底部
android:layout_centerInParent —— 该组件是否相对于父组件居中
android:layout_centerHorizontal —— 该组件是否横向居中
android:layout_centerVertical —— 该组件是否垂直居中
RelativeLayout是Android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。下面示例就展示这么一个情况,第一个文本框与父组件的底部对齐,第二个文本框位于第一个文本框的上方,并且第三个文本框位于第二个文本框的左方。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version= "1.0" encoding= "utf-8" ?> <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <TextView android:id= "@+id/text_01" android:layout_width= "50dp" android:layout_height= "50dp" android:background= "#ffffffff" android:gravity= "center" android:layout_alignParentBottom= "true" android:text= "1" /> <TextView android:id= "@+id/text_02" android:layout_width= "50dp" android:layout_height= "50dp" android:background= "#ff654321" android:gravity= "center" android:layout_above= "@id/text_01" android:layout_centerHorizontal= "true" android:text= "2" /> <TextView android:id= "@+id/text_03" android:layout_width= "50dp" android:layout_height= "50dp" android:background= "#fffedcba" android:gravity= "center" android:layout_toLeftOf= "@id/text_02" android:layout_above= "@id/text_01" android:text= "3" /> <TextView android:id= "@+id/text_04" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:background= "#000000" android:gravity= "center" android:layout_toLeftOf= "@id/text_02" android:layout_above= "@id/text_01" android:text= "RelativeLayout是Android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。" /> </RelativeLayout> |
TableLayout:
TableLayout顾名思义,此布局为表格布局,适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。
TableRow是LinearLayout的子类,它的android:orientation属性值恒为horizontal,并且它的android:layout_width和android:layout_height属性值恒为MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是横向排列,并且宽高一致的。这样的设计使得每个TableRow里的子元素都相当于表格中的单元格一样。在TableRow中,单元格可以为空,但是不能跨列。
下面示例演示了一个TableLayout的布局结构,其中第二行只有两个单元格,而其余行都是三个单元格。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?xml version= "1.0" encoding= "utf-8" ?> <TableLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <TableRow android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <TextView android:background= "#ffffffff" android:gravity= "center" android:padding= "10dp" android:text= "1" /> <TextView android:background= "#ff654321" android:gravity= "center" android:padding= "10dp" android:text= "2" /> <TextView android:background= "#fffedcba" android:gravity= "center" android:padding= "10dp" android:text= "3" /> </TableRow> <TableRow android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <TextView android:background= "#ff654321" android:gravity= "center" android:padding= "10dp" android:text= "2" /> <TextView android:background= "#fffedcba" android:gravity= "center" android:padding= "10dp" android:text= "3" /> </TableRow> <TableRow android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <TextView android:background= "#fffedcba" android:gravity= "center" android:padding= "10dp" android:text= "3" /> <TextView android:background= "#ff654321" android:gravity= "center" android:padding= "10dp" android:text= "2" /> <TextView android:background= "#ffffffff" android:gravity= "center" android:padding= "10dp" android:text= "1" /> </TableRow> <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text="TableLayout顾名思义,此布局为表格布局,适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。 TableRow是LinearLayout的子类,它的android:orientation属性值恒为horizontal,并且它的android:layout_width和android:layout_height属性值恒为MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是横向排列,并且宽高一致的。这样的设计使得每个TableRow里的子元素都相当于表格中的单元格一样。在TableRow中,单元格可以为空,但是不能跨列。 下面示例演示了一个TableLayout的布局结构,其中第二行只有两个单元格,而其余行都是三个单元格。"/> </TableLayout> |
部分文字描述内容有参考:http://www.cnblogs.com/wisekingokok/archive/2011/08/23/2150452.html 和 http://www.cnblogs.com/wisekingokok/archive/2011/08/24/2152004.html
相关代码以及截图等系自己截取。

赠人玫瑰
手留余香
我们曾如此渴望命运的波澜,到最后才发现:人生最曼妙的风景,竟是内心的淡定与从容……我们曾如此期盼外界的认可,到最后才知道:世界是自己的,与他人毫无关系!-杨绛先生
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2012-06-12 hudson安全矩阵config.xml配置问题解决办法
2012-06-12 [转载]hudson--安全设置
2012-06-12 [学习参考]hudson资料
2012-06-12 shell截取小数等