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来代替。所以这里不再详细介绍。

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开发之Bitmap的内存优化详解

    本文来源:转载自: http://mobile.51cto.com/abased-410796.htm 在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitma ...

  5. android开发之wheel控件使用详解

    出门在外生不起病呀,随便两盒药60多块钱.好吧,不废话了,今天我们来看看wheel控件的使用,这是GitHub上的一个开源控件,用起来十分方便,我们可以用它做许多事情,比如做一个自定义的datepic ...

  6. android开发之eclipse调试debug模式详解

     之前我写了一个相关的帖子,但是今天看了一个还是写的比我详细,于是我拿过来和大家分享. 1.在程序中添加一个断点 如果所示:在Eclipse中添加了一个程序断点 在Eclipse中一共有三种添加断 ...

  7. Android开发之ConstraintLayout相对布局

    介绍 一个 ConstraintLayout 是一个 ViewGroup 允许您以灵活的方式定位和调整小部件的方法. 注意: ConstraintLayout 作为支持库提供,您可以在API级别9(G ...

  8. Android开发之UI的编程方式创建

    我们知道,android中一个activity对应一个xml的UI配置文件,除了用xml文件配置的方式创建用户界面外,还可以使用代码编程的方式来创建一个用户界面.如果用户界面需要在运行过程中动态生成的 ...

  9. Android开发笔记之: 数据存储方式详解

    无论是神马平台,神马开发环境,神马软件程序,数据都是核心.对于开发平台来讲,如果对数据的存储有良好的支持,那么对应用程序的开发将会有很大的促进作用.总体的来讲,数据存储方式有三种:一个是文件,一个是数 ...

随机推荐

  1. Java网络编程技术2

    3. UDP数据报通信 UDP通信中,需要建立一个DatagramSocket,与Socket不同,它不存在“连接”的概念,取而代之的是一个数据报包——DatagramPacket.这个数据报包必须知 ...

  2. C# 事件(Event)

    事件(Event) 基本上说是一个用户操作,如按键.点击.鼠标移动等等,或者是一些出现,如系统生成的通知.应用程序需要在事件发生时响应事件.例如,中断.事件是用于进程间通信. 通过事件使用委托 事件在 ...

  3. 五个瓶颈影响你的Asp.Net程序(网站)性能

    在今天的手机设备世界里,生活的节奏继续加快,因此访问你的网站的用户的耐心也在渐渐失去.同时,我提供了非常多的特性,为了防止你的网站变得过时或者廉价,你必须跟上竞争对手.你想赢得访问者的喝彩,但访问者没 ...

  4. C#代码审查工具 StyleCop

    SourceAnalysis (StyleCop)的终极目标是让所有人都能写出优雅和一致的代码,因此这些代码具有很高的可读性. SourceAnalysis (StyleCop)不是代码格式化(代码美 ...

  5. IOS之正则表达式

    在现阶IOS开发的样式越来越多,我们在开发APP的时候难免会遇到对用户的登录和注册进行操作,但是登录注册如果想要做的人性化少不了的就是校验,对当前用户的登录信息进行校验,如果满足要求我们会把用户注册的 ...

  6. 关于NLPIR应用在KETTLE中的探索

    一:什么是NLPIR? NLPIR汉语分词系统(自然语言处理与信息检索共享平台),主要功能包括中文分词:词性标注:命名实体识别:用户词典功能:支持GBK编码.UTF8编码.BIG5编码.新增微博分词. ...

  7. dev -c++ 快捷键

    转自:http://blog.csdn.net/abcjennifer/article/details/7259222 F8:开始调试 F7:进一步执行当前行,并跳到下一行 F4:添加查看 ctrl ...

  8. css选择器顺序的小技巧

    在线演示 本地下载 css的选择器的顺序其实很有意思,如果应用的好的话,可以做一些简单的逻辑出来,配合上css3,就可以尽可能的脱离js了. 这里的演示是一个带有hover事件的四张照片,效果来自一个 ...

  9. 基于Teigha.Net实现CAD到SHP的转换方案

    CAD在测绘领域运用广泛,所以,现在有很多成果都是CAD格式,但其自身存在很多局限性,需将其转为支持更加广泛,存储更加完善的 SHP文件.ArcGIS中直接提供相关转换工具,但不能转换Xdata,Ar ...

  10. IIS 之 连接数、并发连接数、最大并发工作线程数、队列长度、最大工作进程数

    一.IIS连接数 一般购买过虚拟主机的朋友都熟悉购买时,会限制IIS连接数,顾名思义即为IIS服务器可以同时容纳客户请求的最高连接数,准确的说应该叫“IIS限制连接数”. 客户请求的连接内容包括: [ ...