页面的五种布局以及嵌套『Android系列八』
转自:http://blog.csdn.net/dazlly/article/details/7860125
因为学习比较晚,我用的相关版本为SDK4.1、eclipse4.2,而自己看的教材都是低版本的,这造成了细节上的不同,有时候给学习造成了不小的困扰,不过这样也好,通过解决问题获得的知识理解更加深刻一点,这篇文章就是因为版本不同这个原因由来的。
使用上面说的版本新建一个Android项目,然后打开main.xml文件,注意看代码:
- <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" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="@string/hello_world"
- tools:context=".Main" />
- </RelativeLayout>
RelativeLayout,这个就是五种布局的其中之一,而大多数教材上面讲的都是LinearLayout布局,布局的不同造成模拟机界面显示的不同,为了避免再次困然,先把五种布局都了解一下吧。
五个布局对象:RelativeLayout、LinearLayout、TableLayout、FrameLayout、AbsoulteLayout。
布局一:
相对布局RelativeLayout,定义各控件之间的相对位置关系,通过位置属性和各自的ID配合指定位置关系,在指定位置关系时引用的ID必须在引用之前先被定义,否则将出现异常。
layout/main.xml的代码
- <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" >
- <TextView
- android:id="@+id/firstText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFFFF"
- android:text="@string/first" />
- <TextView
- android:id="@+id/secondText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFF00"
- android:text="@string/second" />
- <TextView
- android:id="@+id/thirdText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FF00FF"
- android:text="@string/third" />
- <TextView
- android:id="@+id/fourthText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00FFFF"
- android:text="@string/fourth" />
- </RelativeLayout>
定义了4个文本标签,各自的文本值(下面几种布局使用同样的strings.xml文件)
- <string name="first">First</string>
- <string name="second">Second</string>
- <string name="third">Third</string>
- <string name="fourth">Fourth</string>
为了清晰展示,从一到四标签的背景颜色为白黄红绿,注意看上面没有定义任何相对位置属性,结果:
都重合到一起了,这说明在没有定义相对位置属性的情况下,所有标签都是相对于屏幕左上角布局的,现在更改一下main.xml的代码:
- <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" >
- <TextView
- android:id="@+id/firstText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFFFF"
- android:text="@string/first" />
- <TextView
- android:id="@+id/secondText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFF00"
- android:layout_below="@id/firstText"
- android:text="@string/second" />
- <TextView
- android:id="@+id/thirdText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FF00FF"
- android:layout_below="@id/secondText"
- android:text="@string/third" />
- <TextView
- android:id="@+id/fourthText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00FFFF"
- android:layout_below="@id/thirdText"
- android:text="@string/fourth" />
- </RelativeLayout>
从二开始、每个标签都加了一个属性android:layout_below,意思是该组件位于引用组件的下方,而引用的组件就是这个属性值里面的内容“@id/要引用的id名”。然后再运行一下,看结果:
OK,符合预期,这就是相对布局,下面列出所有的位置属性以及他们代表的意思,参考一下:
android:layout_above 位于引用组件的上方
android:layout_below 位于引用组件的下方
android:layout_toLeftOf 位于引用组件的左方
android:layout_toRightOf 位于引用组件的右方
android:layout_alignParentTop 是否对齐父组件的顶部
android:layout_alignParentBottom 是否对齐父组件的底部
android:layout_alignParentLeft 是否对齐父组件的左端
android:layout_alignParentRight 是否齐其父组件的右端
android:layout_centerInParent 是否相对于父组件居中
android:layout_centerHorizontal 是否横向居中
android:layout_centerVertical 是否垂直居中
另外可能会用到:ViewGroup.LayoutParams.WRAP_CONTENT,在main.java中可以动态添加控件,这个是添加的控件长宽自适应内容。
布局二:
线性布局LinearLayout,按照垂直或者水平的顺序依次排列子元素(如果不指定,默认为水平),每一个子元素都位于前一个元素之后。这样形成单行N列,或者单列N行的布局;如果想要N行N列,可以嵌套使用LinearLayout。先看看效果:
layout/main.xml
- <LinearLayout 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" >
- <TextView
- android:id="@+id/firstText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFFFF"
- android:text="@string/first" />
- <TextView
- android:id="@+id/secondText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFF00"
- android:text="@string/second" />
- <TextView
- android:id="@+id/thirdText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FF00FF"
- android:text="@string/third" />
- <TextView
- android:id="@+id/fourthText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00FFFF"
- android:text="@string/fourth" />
- </LinearLayout>
这里没有指定任何属性,显示如下,可以看到4个标签水平依次排列:
现在略微修改一下代码,在LinearLayout节点内添加属性android:orientation="vertical",显示如下:
再来演示下N行N列的布局方法,使用嵌套结构,还是main.xml代码:
- <LinearLayout 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:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/firstText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFFFF"
- android:text="@string/first" />
- <TextView
- android:id="@+id/secondText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFF00"
- android:text="@string/second" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/thirdText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FF00FF"
- android:text="@string/third" />
- <TextView
- android:id="@+id/fourthText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00FFFF"
- android:text="@string/fourth" />
- </LinearLayout>
- </LinearLayout>
所得效果如下:(注意每个TextView里面的android:layout_width和android:layout_height的属性值,有兴趣的可以试试不同的值显示的不同的效果)
这个布局中还有个android:layout_weight属性限定在水平布局时,不同的控件占的宽度比率,具体规则为:如果水平布局有两个控件,其android:layout_weight属性值分别为n和m,则属性值为n的控件所占的长度比例为总长的n/(n+m),属性值为m的控件所占的长度比例为m/(n+m);属性值越大,占的份额越多。
这里再演示一下,main.xml中四个控件分别加上android:layout_weight=“对应的数字”:
- <LinearLayout 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:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/firstText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="#FFFFFF"
- android:text="@string/first" />
- <TextView
- android:id="@+id/secondText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="2"
- android:background="#FFFF00"
- android:text="@string/second" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/thirdText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="3"
- android:background="#FF00FF"
- android:text="@string/third" />
- <TextView
- android:id="@+id/fourthText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="4"
- android:background="#00FFFF"
- android:text="@string/fourth" />
- </LinearLayout>
- </LinearLayout>
显示效果:
另外还能通过LinearLayout的android:gravity属性或者控件的android:layout_gravity属性,结合LinearLayout的android:orientation属性来设置水平或者垂直居中,这个可以自己多调试一下。
布局三:
表格布局TableLayout,更方便的展示N行N列的布局格式。TableLayout由许多TableRow组成,每个TableRow都代表一行。
TableRow继承自LinearLayout,android:orientation="horizontal",android:layout_width=“match_parent”,android:layout_height =“wrap_content”。里面定义的控件都是横向排列,并且宽高一致的,相当于表格中的单元格,但是不能合并单元格。
看代码,layout/main.xml
- <TableLayout 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" >
- <TableRow
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/firstText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="#FFFFFF"
- android:text="@string/first" />
- <TextView
- android:id="@+id/secondText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="#FFFF00"
- android:text="@string/second" />
- </TableRow>
- <TableRow
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/thirdText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="#FF00FF"
- android:text="@string/third" />
- <TextView
- android:id="@+id/fourthText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="#00FFFF"
- android:text="@string/fourth" />
- </TableRow>
- </TableLayout>
效果图:(有兴趣的可以去掉android:layout_weight属性看看效果)
关于表格布局需要了解的还有:
android:stretchColumns某一列自动填充空白区域
android:shrinkColumns压缩某个列(用于内容过长,横向显示不完全,多余的往下自动扩展)
TableLayout的列序号从0开始
android:gravity设置对齐规则可以多个条件中间用|分开,比如android:gravity=“top|right”,注意|前后不能有空格
布局四:
单帧布局FrameLayout,理解起来最简单,所有的控件都不能指定位置,从左上角对齐依次叠加,后面的控件直接覆盖在前面的控件之上。为了清晰的显示出效果,这里使用两种不同的控件大小展示。
layout/main.xml代码:
- <FrameLayout 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" >
- <TextView
- android:id="@+id/firstText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFFFF"
- android:text="@string/first" />
- <TextView
- android:id="@+id/secondText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FFFF00"
- android:text="@string/second" />
- <TextView
- android:id="@+id/thirdText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#FF00FF"
- android:text="@string/third" />
- <TextView
- android:id="@+id/fourthText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00FFFF"
- android:text="@string/fourth" />
- </FrameLayout>
显示效果:(因为second比fourth长那么一点点,所以露出来一点)
换个代码再演示一下main.xml
- <FrameLayout 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" >
- <TextView
- android:id="@+id/firstText"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#FFFFFF"
- android:text="@string/first" />
- <TextView
- android:id="@+id/secondText"
- android:layout_width="200dp"
- android:layout_height="200dp"
- android:background="#FFFF00"
- android:text="@string/second" />
- <TextView
- android:id="@+id/thirdText"
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:background="#FF00FF"
- android:text="@string/third" />
- <TextView
- android:id="@+id/fourthText"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:background="#00FFFF"
- android:text="@string/fourth" />
- </FrameLayout>
效果:
布局五:
绝对布局AbsoluteLayout,使用android:layout_x和android:layout_y属性来限定控件的位置,左上角坐标为(0,0),各控件位置可以重叠,实际开发中因为限定的位置太过死板而很少用到,实际上我使用的版本已经提示这个布局过期,不推荐使用,这里简单介绍一下。
layout/main.xml代码:
- <AbsoluteLayout 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" >
- <TextView
- android:id="@+id/firstText"
- android:layout_x="50dp"
- android:layout_y="50dp"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:background="#FFFFFF"
- android:text="@string/first" />
- <TextView
- android:id="@+id/secondText"
- android:layout_x="80dp"
- android:layout_y="80dp"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:background="#FFFF00"
- android:text="@string/second" />
- <TextView
- android:id="@+id/thirdText"
- android:layout_x="120dp"
- android:layout_y="100dp"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:background="#FF00FF"
- android:text="@string/third" />
- <TextView
- android:id="@+id/fourthText"
- android:layout_x="180dp"
- android:layout_y="10dp"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:background="#00FFFF"
- android:text="@string/fourth" />
- </AbsoluteLayout>
效果:
布局到此为止,不同的布局之间还可以嵌套,大家有兴趣的话可以自己多尝试尝试。
页面的五种布局以及嵌套『Android系列八』的更多相关文章
- android五种布局模式
Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:LinearLayout (线性布局),FrameLayout(框架布局),AbsoluteLayout(绝对 ...
- 【Android 复习】:Android五种布局的使用方法
---恢复内容开始--- 在Android布局中,有五种常用的布局,下面我们就来学习一下这几种布局的使用方式 1) 线性布局:LinearLayout 2) 帧布局: FrameLayout 3) ...
- 【转】Android UI 五种布局
在一个Android应用中,Layout是开发中的一个很重要环节,Layout是组成UI不可缺少的一部分. ## Android UI 核心类 在Android应用构建UI的方法有以下几种: 单纯使用 ...
- Android常用五种布局
1. FrameLayout(框架布局) 这个布局可以看成是墙脚堆东西,有一个四方的矩形的左上角墙脚,我们放了第一个东西,要再放一个,那就在放在原来放的位置的上面,这样依次的放,会盖住原来的东西.这个 ...
- JSP页面的五种跳转方法
①RequestDispatcher.forward() 是在服务器端起作用,当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet or JSP到另外一个Se ...
- web网页设计五种布局
1.大框套小框布局 2.通栏布局 3.导航栏在主视觉下方的布局 4.左中右布局 5.环绕式布局
- PHP 页面跳转到另一个页面的几种方法分享
如何在 PHP 中从一个页面重定向到另外一个页面呢?今天 清源 为大家列举出了三种办法,供大家来参考. 一.用HTTP头信息 也就是用PHP的HEADER函数.PHP里的HEADER函数的作用就是向 ...
- CSS-三栏响应式布局(左右固宽,中间自适应)的五种方法
代码: <!-- 1 float --> <h3 class="block">第一种方法-float</h3> <div class=&q ...
- 假设高度已知,请写出三栏布局,其中左栏、右栏各为300px,中间自适应的五种方法
假设高度已知,请写出三栏布局,其中左栏.右栏各为300px,中间自适应的五种方法 HTML CSS 页面布局 题目:假设高度已知,请写出三栏布局,其中左栏.右栏各为300px,中间自适应 <!D ...
随机推荐
- bitnami-redmine 一键安装
下载bitnami-redmine https://bitnami.com/stack/redmine 安装 选择语言 设置登陆用户名和密码,数据库用户名root,数据库密码也是这个设置的密码 其他下 ...
- JS--easyui通用验证
// JavaScript Document $.extend($.fn.validatebox.defaults.rules, { Composite_validation: { validator ...
- Mongodb 分片原理
1.主从mongodb 模式 类似,MySQL的主从配置 参照:https://blog.csdn.net/liusong0605/article/details/11551699 mongoDB有 ...
- Maven学习笔记:POM标签大全详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 【代码学习】PYTHON中的静态方法和类方法
一.类方法 是类对象所拥有的方法,需要用修饰器@classmethod来标识其为类方法,对于类方法,第一个参数必须是类对象,一般以cls作为第一个参数(当然可以用其他名称的变量作为其第一个参数,但是大 ...
- spring boot中配置文件中变量的引用
配置文件中 变量的自身引用 ${名称} java文件中引用:非静态变量 之间在变量上面注释@Value("${名称}") 静态变量 在set方法上注释@Value("$ ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 字节交换
大端模式:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,这样的存储模式有点儿类似于把数据当作字符串顺序处理:地址由小向大增加,而数据从高位往低位放 小端模式:指数据的高字节保 ...
- 如何查看NXP产品的供货计划?
大的半导体厂商一般会提供每个产品的生命周期计划,NXP的工业级IC一般供货10年,汽车级是15年,具体的时间可以在官网查询得到. 首先,打开NXP官网链接 产品长期供货计划,可以看到以下页面 接着,筛 ...
- 从数组A中删除在数组B中存在的元素,用C语言实现
从数组A中删除在数组B中存在的元素,用C语言实现 考验数组操作的能力,C语言的熟练程度. //功能:从数组A中删除在数组B中也存在的数据 //输入:arrA --- 数组A // lenA --- 数 ...
- poj1988 Cube Stacking(并查集
题目地址:http://poj.org/problem?id=1988 题意:共n个数,p个操作.输入p.有两个操作M和C.M x y表示把x所在的栈放到y所在的栈上(比如M 2 6:[2 4]放到[ ...