五种布局
2016-02-24 01:48
253 查看
五个布局对象:RelativeLayout、LinearLayout、TableLayout、FrameLayout、AbsoulteLayout。
1. RelativeLayout
在指定位置关系时引用的ID必须在引用之前先被定义,否则将出现异常。
在没有定义相对位置属性的情况下,所有标签都是相对于屏幕左上角布局的。
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中可以动态添加控件,这个是添加的控件长宽自适应内容。
2. LinearLayout
这个布局中还有个android:layout_weight属性限定在水平布局时,不同的控件占的宽度比率,具体规则为:如果水平布局有两个控件,其android:layout_weight属性值分别为n和m,则属性值为n的控件所占的长度比例为总长的n/(n+m),属性值为m的控件所占的长度比例为m/(n+m);属性值越大,占的份额越多。
这里再演示一下,main.xml中四个控件分别加上android:layout_weight=“对应的数字”:
[html] view
plain copy
<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>
显示效果:
![](http://my.csdn.net/uploads/201208/13/1344844474_4655.JPG)
另外还能通过LinearLayout的android:gravity属性或者控件的android:layout_gravity属性,结合LinearLayout的android:orientation属性来设置水平或者垂直居中,这个可以自己多调试一下。
3. TableLayout
表格布局TableLayout,更方便的展示N行N列的布局格式。TableLayout由许多TableRow组成,每个TableRow都代表一行。
TableRow继承自LinearLayout,android:orientation="horizontal",android:layout_width=“match_parent”,android:layout_height =“wrap_content”。里面定义的控件都是横向排列,并且宽高一致的,相当于表格中的单元格,但是不能合并单元格。
看代码,layout/main.xml
[html] view
plain copy
<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属性看看效果)
![](http://my.csdn.net/uploads/201208/13/1344846422_9677.JPG)
关于表格布局需要了解的还有:
android:stretchColumns某一列自动填充空白区域
android:shrinkColumns压缩某个列(用于内容过长,横向显示不完全,多余的往下自动扩展)
TableLayout的列序号从0开始
android:gravity设置对齐规则可以多个条件中间用|分开,比如android:gravity=“top|right”,注意|前后不能有空格
4. FrameLayout
单帧布局FrameLayout,理解起来最简单,所有的控件都不能指定位置,从左上角对齐依次叠加,后面的控件直接覆盖在前面的控件之上。为了清晰的显示出效果,这里使用两种不同的控件大小展示。
layout/main.xml代码:
[html] view
plain copy
<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长那么一点点,所以露出来一点)
![](http://my.csdn.net/uploads/201208/13/1344847031_6736.JPG)
换个代码再演示一下main.xml
[html] view
plain copy
<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>
效果:
![](http://my.csdn.net/uploads/201208/13/1344847230_8271.JPG)
五.
AbsoluteLayout
绝对布局AbsoluteLayout,使用android:layout_x和android:layout_y属性来限定控件的位置,左上角坐标为(0,0),各控件位置可以重叠,实际开发中因为限定的位置太过死板而很少用到,实际上我使用的版本已经提示这个布局过期,不推荐使用,这里简单介绍一下。
layout/main.xml代码:
[html] view
plain copy
<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>
效果:
1. RelativeLayout
在指定位置关系时引用的ID必须在引用之前先被定义,否则将出现异常。
在没有定义相对位置属性的情况下,所有标签都是相对于屏幕左上角布局的。
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中可以动态添加控件,这个是添加的控件长宽自适应内容。
2. LinearLayout
这个布局中还有个android:layout_weight属性限定在水平布局时,不同的控件占的宽度比率,具体规则为:如果水平布局有两个控件,其android:layout_weight属性值分别为n和m,则属性值为n的控件所占的长度比例为总长的n/(n+m),属性值为m的控件所占的长度比例为m/(n+m);属性值越大,占的份额越多。
这里再演示一下,main.xml中四个控件分别加上android:layout_weight=“对应的数字”:
[html] view
plain copy
<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属性来设置水平或者垂直居中,这个可以自己多调试一下。
3. TableLayout
表格布局TableLayout,更方便的展示N行N列的布局格式。TableLayout由许多TableRow组成,每个TableRow都代表一行。
TableRow继承自LinearLayout,android:orientation="horizontal",android:layout_width=“match_parent”,android:layout_height =“wrap_content”。里面定义的控件都是横向排列,并且宽高一致的,相当于表格中的单元格,但是不能合并单元格。
看代码,layout/main.xml
[html] view
plain copy
<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”,注意|前后不能有空格
4. FrameLayout
单帧布局FrameLayout,理解起来最简单,所有的控件都不能指定位置,从左上角对齐依次叠加,后面的控件直接覆盖在前面的控件之上。为了清晰的显示出效果,这里使用两种不同的控件大小展示。
layout/main.xml代码:
[html] view
plain copy
<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
[html] view
plain copy
<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
绝对布局AbsoluteLayout,使用android:layout_x和android:layout_y属性来限定控件的位置,左上角坐标为(0,0),各控件位置可以重叠,实际开发中因为限定的位置太过死板而很少用到,实际上我使用的版本已经提示这个布局过期,不推荐使用,这里简单介绍一下。
layout/main.xml代码:
[html] view
plain copy
<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>
效果:
相关文章推荐
- myeclipse/eclipse添加Spket插件实现ExtJs4.2/ExtJs3智能提示
- 我拿什么来爱你
- 假期与书
- java 消息机制 ActiveMQ入门实例
- 消息队列MQ】各类MQ比较
- 问卷调查
- Bit field
- 基于FPGA的图像处理--基本概念导图
- kidd风的IOS日志之GCD详解
- PullToRefreshListView 应用讲解
- MySQL 数据库设计初步规范
- 关于错误样式遮挡点击事件问题
- 21. Merge Two Sorted Lists
- 拼接JS方法参数异常问题
- 大型数据库设计原则
- 让网页图片变灰色的三种方法
- 数据库设计14个技巧
- MyBatis下动态语句使用like进行模糊查询
- kidd风的IOS日志之iOS开发多线程篇—多线程简单介绍
- uvaoj-12096:the setstack computer