Android布局是应用界面开发的重要一环,在Android中,共有四种布局方式,

分别是:FrameLayout( 帧布局 )、LinearLayout (线性布局)、TableLayout(表格布局)、RelativeLayout(相对布局)。

1.LinearLayout

线性布局LinearLayout 在单一方向上对齐所有的子视图-竖向或者横向,这依赖于你怎么定义方向orientation 属性。所有子视图依次堆积,所以一个竖向列表每行只有一个子视图,不管它们有多宽,而一个横向列表将只有一行高(最高子视图的高度,加上填充)。一个线性布局LinearLayout 会考虑子视图之间的边缘空白margins以及每个子视图的引力属性(靠右,居中,或者靠左)。

2.RelativeLayout

相对布局RelativeLayout允许子视图指定它们和父视图或彼此之间的相对位置(通过ID指定)。因此你可以按正确的顺序对齐两个元素,或者让一个视图在另外一个下面,居于屏幕中间,左边的中间,等等。元素通过给定顺序来绘制,因此如果这第一个元素在屏幕中间,其他以它对齐的元素都会对齐到屏幕中间。同样,因为这个顺序,如果使用XML来指定这个布局,你将引用的元素(为了定位其它视图对象)必须被列在XML文件中,在你通过引用ID从其他视图中引用它之前。

其中一些特性直接由元素支持,另外一些由它的LayoutParams成员变量支持(为所有这个屏幕中的元素子类化RelativeLayout,因为所有元素都是RelativeLayout父对象的子元素)。已定义的相对布局RelativeLayout参数是:width,height,below,alignTop,toLeft,padding[Bottom|Left|Right|Top],以及 margin[Bottom|Left|Right|Top]。注意其中一些参数明确支持相对布局位置-它们的数值必须是你的相对位置元素的ID。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvPhone"
android:id="@+id/tv1" /> <EditText
android:id="@+id/editPh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv1"
android:layout_below="@+id/tv1"
android:hint="@string/phoneMsg"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editPh"
android:layout_below="@+id/editPh"
android:text="@string/tvSMS" />
<EditText
android:id="@+id/etSMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etSMS"
android:layout_below="@+id/etSMS"
android:text="@string/send" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/etSMS"
android:text="Button" /> </RelativeLayout>

3.TableLayout

表格布局TableLayout把它的子视图定位到行和列中。表格布局容器不显示行,列和单元的边界线。表的列和最多行单元数一样多。一个表可以有空单元,但是单元不能像HTML里面那样跨列。

TableRow 对象是一个TableLayout的子视图(每个TableRow定义了表中的一个单独行)。每行有0或多个单元,可用任何其他视图定义。因此,行单元可能由各个视图对象组成,如ImageView或TextView对象。一个单元也可以是一个ViewGroup对象(比如,你可以嵌入另一个表布局作为一个单元)。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="60dp"
android:gravity="center_vertical"
android:text="@string/tvPhone"
android:id="@+id/tv1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvPhone"
android:id="@+id/tv2" />
</TableRow>
</TableLayout>

4.FrameLayout

FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/movie"
android:contentDescription="@string/movie"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/paly"
android:layout_gravity="center"
android:contentDescription="@string/play"/>
</FrameLayout>

android:gravity设置空间中内容的对齐方式,android:layout_gravity设置控件本身的对齐方式

当android:gravity和android:padding同时做用时,先按gravity进行排布,再按padding调整

android:padding指定内边距,android:layout_margin指定外边距

Android学习笔记_3_四种布局的更多相关文章

  1. 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版

    目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...

  2. 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果

    目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...

  3. 【转】Pro Android学习笔记(四):了解Android资源(下)

    处理任意的XML文件 自定义的xml文件放置在res/xml/下,可以通过R.xml.file_name来获取一个XMLResourceParser对象.下面是xml文件的例子: <rootna ...

  4. Android学习笔记(九)——布局和控件的自定义

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! View是 Android中一种最基本的 UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件 ...

  5. Android学习笔记(四)深入探讨Activity

    在应用程序中至少包含一个用来处理应用程序的主UI功能的主界面屏幕.这个主界面一般由多个Fragment组成,并由一组次要Activity支持.要在屏幕之间切换,就必须要启动一个新的Activity.一 ...

  6. Android学习笔记(四)——再探Intent

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 我们可以使用 Intent 来启动一个活动, 还可以在启动活动的时候传递数据的,下面一起来看一下: 一.向下一 ...

  7. Android学习笔记六:六大布局

    六大界面布局方式包括: 线性布局(LinearLayout).帧布局(FrameLayout).表格布局(TableLayout).相对布局(RelativeLayout).绝对布局(Absolute ...

  8. 【转】 Pro Android学习笔记(四八):ActionBar(1):Home图标区

    目录(?)[-] Home Icon 源代码 TextView的滚动 返回主activity或指定activity     ActionBar在Android 3.0 SDK中为平板引入,在4.0中也 ...

  9. 【转】 Pro Android学习笔记(四七):Dialog(4):一些补充和思考

    目录(?)[-] 编程思想封装接口 fragment和activity以其他fragment之间的通信 编程思想:封装接口 在小例子中,fragment会调用activity的onDialogDone ...

随机推荐

  1. 【ORACLE】sqlplus使用记录

    1.设置输出长度 SEGMENT_NAME--------------------------- BYTES----------TZ01_LOGIN_DATA 20971520 TZ02_EP_GAT ...

  2. go语言解析网页利器goquery使用教程(爬虫必备)

    某些时候需要爬取网页中指定信息时,通常需要一些框架解析网页行成dom模型,然后来操作节点来获取相应的信息.在java中很显然就是Jsoup,而在Golang里,应该就是这个goquery了吧. goq ...

  3. HDU 1800——Flying to the Mars——————【字符串哈希】

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. python pickle命令执行与marshal 任意代码执行

    1.python pickle反序列化漏洞 自己的理解: 由于在类的__reduce__方法中提供了我们可以自定义程序如何去解序列化的方法,因此如果应用程序接受了不可信任的序列化的数据,那么就可能导致 ...

  5. SQL脚本整理系列 三

    触发器 SQL 2008 怎么实现删除学生表里面的一条记录,成绩表里面关于这个学生的记录也同时删掉,谢求具体代码 --创建表 DROP TABLE tstudent GO CREATE TABLE t ...

  6. Javascript怎么跳出循环,嵌套循环。

    今天要实现一个功能,在数组a中的每一项,对应数组b中的每一项,如果对应上了就给数组b的checked增加ture属性,如果查找不到就给数组b的checked增加false属性. 如果有哪里写的不对欢迎 ...

  7. 简单springboot及springboot cloud环境搭建

    springboot使用特定的方式,简化了spring的各种xml配置文件,并通过maven或者gradle,完成所需依赖,使用springboot maven插件,可直接输出可运行的jar包,省去了 ...

  8. ArrayList集合长度的问题

    // 每次集合中实际包含的元素个数(count)超过了可包含元素的个数capcity  //的时候集合就会向内存中申请多开启一倍的空间,来保证集合长度够用 static void Main(strin ...

  9. SQL Server与Oracle有什么区别?

    1.可操作平台上: Oracle可在所有主流平台上运行,Oracle数据库采用开放的策略目标,它使得客户可以选择一种最适合他们特定需要的解决方案.客户可以利用很多种第三方应用程序.工具.而SQL Se ...

  10. 重构指南 - 引入参数对象(Introduce Parameter Object)

    当一个方法的参数超过3个以上,就可以考虑将参数封装成一个对象.将参数封装成对象后提高了代码的可读性,并且该参数对象也可以供多个方法调用,以后如果增加删除参数,方法本身不需要修改,只需要修改参数对象就可 ...