Android中常用的5大布局方式有以下几种:

  • 线性布局(LinearLayout):按照垂直或者水平方向布局的组件。
  • 帧布局(FrameLayout):组件从屏幕左上方布局组件。
  • 表格布局(TableLayout):按照行列方式布局组件。
  • 相对布局(RelativeLayout):相对其它组件的布局方式。
  • 绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。
 
1. 线性布局

线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。

常用的属性:

android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小

第一个实例

①效果图:

②核心代码如下:

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="vertical"
  11. >
  12. <EditText
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. />
  16. </LinearLayout>
  17. <LinearLayout
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:orientation="horizontal"
  21. android:gravity="right"
  22. >
  23. <!-- android:gravity="right"表示Button组件向右对齐 -->
  24. <Button
  25. android:layout_height="wrap_content"
  26. android:layout_width="wrap_content"
  27. android:text="确定"
  28. />
  29. <Button
  30. android:layout_height="wrap_content"
  31. android:layout_width="wrap_content"
  32. android:text="取消"
  33. />
  34. </LinearLayout>
  35. </LinearLayout>

第二个实例

①效果图:

②核心代码:

mian.xml
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayout
  6. android:orientation="horizontal"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:layout_weight="1">
  10. <TextView
  11. android:text="red"
  12. android:gravity="center_horizontal"
  13. android:background="#aa0000"
  14. android:layout_width="wrap_content"
  15. android:layout_height="fill_parent"
  16. android:layout_weight="1"
  17. />
  18. <!--android:gravity="center_horizontal"水平居中 -->
  19. <!--layout_weight属性以控制各个控件在布局中的相对大小。layout_weight属性是一个非负整数值。
  20. 线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。
  21. 例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,
  22. 那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;
  23. 对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,
  24. 再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度-->
  25. <TextView
  26. android:text="Teal"
  27. android:gravity="center_horizontal"
  28. android:background="#008080"
  29. android:layout_width="wrap_content"
  30. android:layout_height="fill_parent"
  31. android:layout_weight="1"/>
  32. <TextView
  33. android:text="blue"
  34. android:gravity="center_horizontal"
  35. android:background="#0000aa"
  36. android:layout_width="wrap_content"
  37. android:layout_height="fill_parent"
  38. android:layout_weight="1"
  39. />
  40. <TextView
  41. android:text="orange"
  42. android:gravity="center_horizontal"
  43. android:background="#FFA500"
  44. android:layout_width="wrap_content"
  45. android:layout_height="fill_parent"
  46. android:layout_weight="1"
  47. />
  48. </LinearLayout>
  49. <LinearLayout
  50. android:orientation="vertical"
  51. android:layout_width="fill_parent"
  52. android:layout_height="fill_parent"
  53. android:layout_weight="1">
  54. <TextView
  55. android:text="row one"
  56. android:textSize="15pt"
  57. android:background="#aa0000"
  58. android:layout_width="fill_parent"
  59. android:layout_height="wrap_content"
  60. android:layout_weight="1"
  61. />
  62. <!--  -->
  63. <TextView
  64. android:text="row two"
  65. android:textSize="15pt"
  66. android:background="#DDA0DD"
  67. android:layout_width="fill_parent"
  68. android:layout_height="wrap_content"
  69. android:layout_weight="1"
  70. />
  71. <TextView
  72. android:text="row three"
  73. android:textSize="15pt"
  74. android:background="#008080"
  75. android:layout_width="fill_parent"
  76. android:layout_height="wrap_content"
  77. android:layout_weight="1"
  78. />
  79. <TextView
  80. android:text="row four"
  81. android:textSize="15pt"
  82. android:background="#FFA500"
  83. android:layout_width="fill_parent"
  84. android:layout_height="wrap_content"
  85. android:layout_weight="1"
  86. />
  87. </LinearLayout>
  88. </LinearLayout>
2. 帧布局
帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。
 
 简单的例子
①效果图:
 
 
② 核心代码:
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <TextView
  7. android:layout_width="300dp"
  8. android:layout_height="300dp"
  9. android:background="#00BFFF"
  10. />
  11. <TextView
  12. android:layout_width="260dp"
  13. android:layout_height="260dp"
  14. android:background="#FFC0CB"
  15. />
  16. <TextView
  17. android:layout_width="220dp"
  18. android:layout_height="220dp"
  19. android:background="#0000FF"
  20. />
  21. </FrameLayout>
 
3.表格布局
表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数
 
简单的列子:
①效果图:
 
 
② 核心代码:
 main.xml
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <TableRow>
  7. <Button
  8. android:text="Button1"
  9. />
  10. <Button
  11. android:text="Button2"
  12. />
  13. <Button
  14. android:text="Button3"
  15. />
  16. </TableRow>
  17. <TableRow>
  18. <Button
  19. android:text="Button4"
  20. />
  21. <Button
  22. android:layout_span="2"
  23. android:text="Button5"
  24. />
  25. </TableRow>
  26. </TableLayout>
 
4.相对布局
相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。
相对布局常用属性请参考我博客的:http://liangruijun.blog.51cto.com/3061169/631816
 
简单的例子
①效果图:
 
 
② 核心代码:
main.xml
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:padding="10px"
  6. >
  7. <TextView
  8. android:id="@+id/tev1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_marginBottom="30dp"
  12. android:text="Please Type Here:"
  13. />
  14. <EditText
  15. android:id="@+id/tx1"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_below="@id/tev1"
  19. />
  20. <Button
  21. android:id="@+id/btn1"
  22. android:layout_height="wrap_content"
  23. android:layout_width="wrap_content"
  24. android:layout_below="@id/tx1"
  25. android:layout_alignParentRight="true"
  26. android:text="确定"
  27. />
  28. <Button
  29. android:id="@+id/btn2"
  30. android:layout_height="wrap_content"
  31. android:layout_width="wrap_content"
  32. android:layout_below="@id/tx1"
  33. android:layout_toLeftOf="@id/btn1"
  34. android:layout_marginRight="30dp"
  35. android:text="取消"
  36. />
  37. </RelativeLayout>
5. 绝对布局
 绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里不再详细介绍。
 
from:http://liangruijun.blog.51cto.com/3061169/632532/

【转】Android开发学习笔记:5大布局方式详解的更多相关文章

  1. 【Android开发学习笔记之一】5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  2. Android开发5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  3. Android开发之5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  4. android开发学习笔记000

    使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...

  5. IP地址和子网划分学习笔记之《IP地址详解》

    2018-05-03 18:47:37   在学习IP地址和子网划分前,必须对进制计数有一定了解,尤其是二进制和十进制之间的相互转换,对于我们掌握IP地址和子网的划分非常有帮助,可参看如下目录详文. ...

  6. 零拷贝详解 Java NIO学习笔记四(零拷贝详解)

    转 https://blog.csdn.net/u013096088/article/details/79122671 Java NIO学习笔记四(零拷贝详解) 2018年01月21日 20:20:5 ...

  7. Android Studio 学习笔记(二):布局简介和xmlns说明

    初学Android Studio,是在b站看的教程视频,这里的笔记也是以其为基础的,个人强烈安利: [天哥]Android开发视频教程最新版 Android Studio开发 Android 布局简介 ...

  8. 【Android开发学习笔记】【第八课】五大布局-下

    概念 五大布局上一篇文章已经介绍了 LinearLayout RelativeLayout 这一篇我们介绍剩下的三种布局 FrameLayout 五种布局中最佳单的一种布局.在这个布局在整个界面被当成 ...

  9. 【Android开发学习笔记】【第七课】五大布局-上

    概念 Android程序各式各样,依靠的就是布局,先来看看布局都是怎么来的: 白色部分就是我们经常用的几种布局,主要说说介绍下面五大布局 FrameLayout AbsoluteLayout Line ...

随机推荐

  1. 查看 dmp 字符集

    用Oracle的exp工具导出的dmp文件也包含了字符集信息,dmp文件的第2和第3个字节记录了dmp文件的字符集.如果dmp文件不大,比如只有几M或几十M,可以用UltraEdit打开(16进制方式 ...

  2. Javascript实现两张图片的延迟加载

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. java基础知识回顾之java Thread类学习(四)--java多线程安全问题(锁)

    上一节售票系统中我们发现,打印出了错票,0,-1,出现了多线程安全问题.我们分析为什么会发生多线程安全问题? 看下面线程的主要代码: @Override public void run() { // ...

  4. 2013 Multi-University Training Contest 1 3-idiots

    解题报告: 记录 A_i 为长度为 i 的树枝的数量,并让 A 对它本身做 FFT,得到任意选两个树枝能得到的各个和的数量.枚举第三边, 计算出所有两边之和大于第三条边的方案数,并把前两条边包含最长边 ...

  5. secure CRT 介绍

        SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件.     Secure[1] CRT支持SSH,同时 ...

  6. 使用getScript()方法异步加载并执行js文件

    使用getScript()方法异步加载并执行js文件 使用getScript()方法异步请求并执行服务器中的JavaScript格式的文件,它的调用格式如下所示: jQuery.getScript(u ...

  7. NOIP 2015提高组复赛

    神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第 ...

  8. Apache与Tomcat整合

    Apache与Tomcat整合   一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache ...

  9. amd(超微半导体公司(英语:Advanced Micro Devices, Inc.,缩写:AMD))

    公司名称 AMD(超微半导体公司)   经营范围 CPU.显卡.主板等电脑硬件设备 AMD公司专门为计算机.通信和消费电子行业设计和制造各种创新的微处理器(CPU.GPU.APU.主板芯片组.电视卡芯 ...

  10. Qt 自定义model实现文件系统的文件名排序(重定义sort函数即可。忽然开窍了:其实捕捉点击Header事件,内部重排序,全部刷新显示即可)

    前段时间,需要做一个功能是要做文件系统的排序的功能.由于是自己写的model, 自己定义的数据结构.最初的想法只有一个自己去实现文件夹跟文件名的排序算法,不过感觉比较费时间.后来想到的是QFileSy ...