Android布局大全
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦。组件按照布局的要求依次排列,就组成了用户所看见的界面。
所有的布局方式都可以归类为ViewGroup的5个类别,即ViewGroup的5个直接子类。其它的一些布局都扩展自这5个类。
1.LinearLayout,线性布局方式
这种布局比较常用,也比较简单,就是每个元素占一行,当然也可能声明为横向排放,也就是每个元素占一列。
LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。
LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。android:layout_weight遵循数值越小,重要度越高的原则。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff000000" android:text="@string/hello"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff654321" android:layout_weight="1" android:text="1"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fffedcba" android:layout_weight="2" android:text="2"/>
</LinearLayout>
</LinearLayout>
2.Relative Layout,相对布局
RelativeLayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below, android:layout_above, android:layout_centerVertical等。注意在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。
RelativeLayout是Android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/text_01" android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_alignParentBottom="true" android:text="1"/>
<TextView android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_above="@id/text_01" android:layout_centerHorizontal="true" android:text="2"/>
<TextView android:id="@+id/text_03" android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_toLeftOf="@id/text_02" android:layout_above="@id/text_01" android:text="3"/>
</RelativeLayout>
3.AbsoluteLayout,绝对位置布局
在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。在此布局中的子元素可以相互重叠。在实际开发中,通常不采用此布局格式,因为它的界面代码过于刚性,以至于有可能不能很好的适配各种终端。
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_x="50dp" android:layout_y="50dp" android:text="1"/>
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_x="25dp" android:layout_y="25dp" android:text="2"/>
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_x="125dp" android:layout_y="125dp" android:text="3"/>
</AbsoluteLayout>
4.FrameLayout,帧布局
FrameLayout是五大布局中最简单的一个布局,可以说成是层布局方式。在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二个TextView的部分位置。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/>
<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff654321" android:gravity="center" android:text="2"/>
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:text="3"/>
</FrameLayout>
5.TableLayout,表格布局
适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。
TableRow是LinearLayout的子类,ablelLayout并不需要明确地声明包含多少行、多少列,而是通过TableRow,以及其他组件来控制表格的行数和列数, TableRow也是容器,因此可以向TableRow里面添加其他组件,没添加一个组件该表格就增加一列。如果想TableLayout里面添加组件,那么该组件就直接占用一行。在表格布局中,列的宽度由该列中最宽的单元格决定,整个表格布局的宽度取决于父容器的宽度(默认是占满父容器本身)。
TableLayout继承了LinearLayout,因此他完全可以支持LinearLayout所支持的全部XML属性,除此之外TableLayout还支持以下属性:
XML属性 相关用法 说明
1. andriod:collapseColumns setColumnsCollapsed(int ,boolean) 设置需要隐藏的列的序列号,多个用逗号隔开
2.android:shrinkColumns setShrinkAllColumns(boolean) 设置被收缩的列的序列号,多个用逗号隔开
3.android:stretchColimns setSretchAllColumnds(boolean) 设置允许被拉伸的列的序列号,多个用逗号隔开
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>
<TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
<TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
<TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
<TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
<TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>
</TableRow>
</TableLayout>
6.其他布局(隶属关系请看上图)
1)列表视图(List View)
List View是可滚动的列表。以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。
具体应用请看:用法一 http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html
用法二 http://blog.csdn.net/koupoo/article/details/7018727
2)网格视图(Grid View)
Grid View一个ViewGroup以网格显示它的子视图(view)元素,即二维的、滚动的网格。
具体应用查看:http://www.cnblogs.com/linzheng/archive/2011/01/19/1938760.html
3)标签布局(Tab Layout)
以标签的方式显示它的子视图元素,就像在Firefox中的一个窗口中显示多个网页一样。为了狂创建一个标签UI(tabbed UI),需要使用到TabHost
和TabWidget。
TabHost
必须是布局的根节点,它包含为了显示标签的TabWidget和显示标签内容的
FrameLayout
。
具体应用查看:http://www.cnblogs.com/devinzhang/archive/2012/01/18/2325887.html
Android布局大全的更多相关文章
- Android之布局大全
Android布局方式可以归类为ViewGroup的5个类别,即ViewGroup的5个直接子类.其它的一些布局都扩展自这5个类. 上结构图: 转自:http://www.cnblogs.com/de ...
- Android布局详解之一:FrameLayout
原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...
- Android布局及属性归总(查询用)
常见布局 LinearLayout 线性布局 子元素任意,组织成一个单一的水平或垂直行,默认为水平方向TableLayout 表格布局 子元素为<Tabl ...
- [转]最流行的android组件大全
目录(?)[+] 目录(?)[-] 最流行的android组件大全 UI组件 类库 游戏引擎 Android HTML5应用 最流行的android组件大全 最新更新文档请访问我的网站 Andro ...
- (转载) android项目大全,总有你所需的
目录视图 摘要视图 订阅 赠书 | 异步2周年,技术图书免费选 程序员8月书讯 项目管理+代码托管+文档协作,开发更流畅 [置顶] android项目大全,总有你所需的 标签: 源 ...
- android 权限大全
教程 博客 淘帖 论坛›eoe·Android开发资源区›Android开发实例教程 191507 12 / 2 页下一页 android 权限大全 『癲瘋霸気』 于 2013-4-3 10: ...
- 【转】在Android布局中使用include和merge标签
内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...
- Android成长日记-Android布局优化
Android常用布局 1. LinearLayout(线性布局) 2. RelativeLayout(相对布局) 3. TableLayout(表格布局) 4. AbsoluteLayou(绝对布局 ...
- 【转】Android布局优化之ViewStub
ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...
随机推荐
- WCF中Service Configuration Editor的使用方法
1.在App.config文件上右击,选择Edit WCF Configuration.... 或者打开Program Files\Microsoft Visual Studio 8\Common7\ ...
- C与C++在const用法上的区别
首先,C和C++在大体结构上不同,却在语法上相同. 所以在使用的时候,我们会时常遇到一些莫名其妙的问题,觉得语法上是正确的,但是编译的时候却出现一个红色的 error! 比如今天我遇到的这个有意思的 ...
- iOS机器学习-TensorFlow
人工智能.机器学习都已走进了我们的日常,尤其是愈演愈热的大数据更是跟我们的生活息息相关,做 人工智能.数据挖掘的人在其他人眼中感觉是很高大上的,总有一种遥不可及的感觉,在我司也经常会听到数据科学部的同 ...
- SQL批量删除与批量插入
批量删除: DELETE FROM MyTable WHERE ID IN (1,2); 批量插入: INSERT INTO MyTable(ID,NAME) VALUES(1,'123');INSE ...
- GPOR
[tengzhenzhen15@lu01 gpor]$ for ((i=0; i<=19; i++)) do ./gpor -S 0.4 X4058_300_gpor/mytask_train. ...
- 转: CSS中overflow的用法
Overflow可以实现隐藏超出对象内容,同时也有显示与隐藏滚动条的作用,overflow属性有四个值:visible (默认), hidden, scroll, 和auto.同样有两个overflo ...
- Blackhat EU 2013 黑客大会(Full Schedule for Black Hat USA 2013)
大会文档下载:https://www.blackhat.com/eu-13/archives.html 此次BH EU 议题整体较水,涉及系统安全.移动安全.网络传输安全.WEB安全.游戏安全等.下面 ...
- 关于Android的onResume的2点体会(程序切换之后恢复状态)
Android有点儿差劲:按home键之后,立即长按home键选择程序切换回来,居然activity就跑回初始状态去了. 我的程序里面有2个webview,2个按钮,我做到把他们都恢复了. 1 web ...
- Redis系列-存储篇hash主要操作函数小结
阳光透过玻璃,洒在身上,一杯暖茶在手,说不尽的安逸自得,让我有种想再写篇blog的冲动.上篇主要谈了string,这里谈谈hash吧!hash是一些列key value(field value)的映射 ...
- hadoop源码阅读
1.Hadoop的包的功能分析 2.由于Hadoop的MapReduce和HDFS都有通信的需求,需要对通信的对象进行序列化.Hadoop并没有采用java的序列化,而是引入它自己的系统.org.ap ...