http://bbs.chinaunix.net/thread-3654213-1-1.html

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

LinearLayout(线性布局)

TableLayout(表格布局)

RelativeLayout(相对布局)

AbsoluteLayout(绝对布局)

FrameLayout(框架布局)

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

一,LinearLayout(线性布局)

在一个方向上(垂直或水平)对齐所有子元素
一个垂直列表每行将只有一个子元素(无论它们有多宽)
一个水平列表只是一列的高度(最高子元素的高度来填充)

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

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

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

复制代码

三、RelativeLayout(相对布局)

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

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

复制代码

四、AbsoluteLayout(绝对布局)

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

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

复制代码

五、FrameLayout(框架布局)

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

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

Android开发之详解五大布局的更多相关文章

  1. Android开发实例详解之IMF(Android SDK Sample—SoftKeyboard)

    本博前面的文章介绍了Android开发环境的搭建和模拟器的常用操作.本次,将以Android Sample中经典的SoftKeyboard项目为例,详细解析Android上一个小型项目的开发过程和注意 ...

  2. Android开发——AsyncTask详解

    android提供AsynvTask,目的是为了不阻塞主线程(UI线程),且UI的更新只能在主线程中完成,因此异步处理是不可避免的. Android为了降低开发难度,提供了AsyncTask.Asyn ...

  3. Android开发 Context详解与类型 转载

    转载地址:https://blog.csdn.net/guolin_blog/article/details/47028975 个人总结: Context分为 activity : activity其 ...

  4. Android 开发 ConstraintLayout详解

    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' app:layout_constraintHorizo ...

  5. Android开发 GradientDrawable详解

    前言 GradientDrawable类似与Xml布局里的shape,常用在一些自己封装的对话框控件的背景或者其他View中,优势是不需要你在带着xml布局文件一起封包.. 画线 GradientDr ...

  6. Android 开发 HandlerThread详解 转载

    转载请注明出处:http://blog.csdn.net/vnanyesheshou/article/details/75073307 对于Handler不太懂的可以参考我的这两篇文章: Androi ...

  7. Android开发 StateListDrawable详解

    前言 StateListDrawable是与xml中的selector属性对应代码实现类,它需要配合GradientDrawable的使用,如果你还不了解GradientDrawable可以参考我的另 ...

  8. Android开发 layer-list详解

    参考:https://blog.csdn.net/speverriver/article/details/80925686 挖坑,以后填坑

  9. Android开发 LevelListDrawable详解

    前言 此篇博客正在施工中... 作者其实就是想挖个坑备忘一下... 十分抱歉, 可以参考https://www.jianshu.com/p/f9ec65241b6b

随机推荐

  1. Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建

    1.开发工具使用: MyEclipse或Eclipse,数据库使用Oracle.需要用到的软件有Zookeeper(注册中心),Tomcat(Web容器)和Maven(包管理). 2.初始环境配置: ...

  2. Jquery自定义图片上传插件

    1 概述 编写后台网站程序大多数用到文件上传,可是传统的文件上传控件不是外观不够优雅,就是性能不太好看,翻阅众多文件上传控件的文章,发现可以这样去定义一个文件上传控件,实现的文件上传的效果图如下: 2 ...

  3. SQLAlchemy Core插入数据,有好几种方法呢

    看是一次插入一条还是多条, 看是数据表名是变量还是常量, 操作还是很灵活的, 主要看哪种顺手顺眼啦. #coding=utf-8 from datetime import datetime from ...

  4. BAT批量处理 命令

    第一章 批处理基础第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文件是将一系列命令按一定的顺序集合为一个可执行的文本文件,其扩展名为BAT或者CMD.这些命令统称批处理命令.小知识:可以 ...

  5. hdu 4049 2011北京赛区网络赛J 状压dp ***

    cl少用在for循环里 #include<cstdio> #include<iostream> #include<algorithm> #include<cs ...

  6. 自己动手写RTP服务器——关于RTP协议

    转自:http://blog.csdn.net/baby313/article/details/7353605 本文会带领着你一步步动手实现一个简单的RTP传输服务器,旨在了解RTP流媒体传输协议以及 ...

  7. BuildFilePath 及打开文件对话框

    也许以后就主要在这里发SOUI的介绍了. 贴一段文件相关的helper, 测试一下贴代码是不是方便. /** * Copyright (C) 2014-2050 * All rights reserv ...

  8. Error: Could not find or load main class test.EditFile

    今天写了一个简单的小程序,运行之后发现Error: Could not find or load main class test.EditFile,项目无法启动.删除main中的所有内容之后依旧提示该 ...

  9. Axure 全局辅助线(转)

    普通辅助线作用于当前页 全局作用于所有页面 , 包括新建页面 创建普通辅助线直接拉出来 创建全局辅助线 , 在拉出来的时候按住 Ctrl 默认情况下 , 颜色不同 辅助线可以多选 , 用拖选 或 按 ...

  10. UNIX索引技术访问文件初阶

    背景: 软考里面,多次碰到一道题: 过程 以前对于这样的题,仅仅知道: 在文件系统中,文件的存储设备通常划分为若干个大小相等的物理块,每块长为512或1024字节.文件的理结构是指文件在存储设备上的存 ...