android开发中的UI控制(六)
android开发中的ui控制(六) 转载自 : http://www.android777.com/index.php/tutorial/androids-ui-control-f.html 之前介绍了android中的文本控制、按钮控制、列表控制和其他一些有趣的控件。大家应该对这些基本的控制控件有一些了解了,但是如何把这些控制控件合成在一个界面中,控制整体布局呢?这时候就需要了解android中的布局管理控件。 在android中几个常见的布局控制对象:linearlayout、tablelayout、relativelayout、framelayout和absolutelayout(deprecated),他们都继承了viewgroup,作为各种不同布局管理模型的容器,它们都提供了各自独到的功能。
framelayout:
framelayout是一个最简单的布局文件,它将子视图放在它的左上方,然后整个容器只能显示一个子视图,如果有多个子视图则他们会像卡片一样重叠在一起只有最上面的能被看到,不过如果你设置它的背景是透明的,则可以透过上面的卡片看到下面的内容。 linearlayout: linearlayout是一个最常用的基础布局对象。它通过设置orientation将内部的所有子视图以横向或纵向进行排列。你将多个横向的linearlayout以当成子视图放进一个纵向的linearlayout中,这样就形成一个类似table的布局,不过如果是这种情况,最好是用tablelayout比较合适。 下面布局文件就是一个纵向布局的例子:
<?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:text="姓名:" /> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="周杰伦" /> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="照片:" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/js1" /></linearlayout>
视图效果如下:
tablelayout布局管理对象会将容器内部的子节点按照行列进行排序,它就像是html中的节点。在tablelayout节点内部中声明将为table添加一行,中的子视图将排成n列,如果内有n个子视图就将该行划分为n列。整个表的列是是根据表格每行中最多有多少列来决定的,假设整个表格中有2行是3列,1行是4列则表格将分为4列。
还有一个需要注意的特性是:tablelayout默认会将所有的子节点当成它的一行数据,而且你可以放任意的view到tablelayout中,默认它会将它的直接子节点当成一行,所以你可以定义任意view对象在tablelayout中,它们默认会各自占用一行,而且你对于tablelayout的直接子节点设置 layout_width=”wrap_content”属性将不会生效,因为默认它会重写成fill_parent。
tablelayout还有一个特殊的属性:stretchcolumns用来设置需要拉宽的列,因为有的列可能不会填满区域,为了布局的美观你可以定义哪个列将进行拉宽。用逗号隔开的列值来指定哪些列需要拉宽,列值是从0开始的,如果你需要拉升第2和第3列你可以设置 stretchcolumns=”1,2″。同样的道理,如果你需要紧缩一个列你可以使用:shrinkcolumns来指定, 如果你要让某些列不可见,可以通过设置collapsecolumns属性,这些的用法都跟stretchcolumns一样。
<?xml version="1.0" encoding="utf-8"?><tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchcolumns="1" > <tablerow> <textview android:text="open..." /> <textview android:text="ctrl-o" android:gravity="right" /> </tablerow> <tablerow> <textview android:text="save as..." /> <textview android:text="ctrl-shift-s" android:gravity="right" /> </tablerow> </tablelayout>
relativelayout:
relativelayout是一个比较复杂、难用的布局管理对象,它是用定义各个视图相对的位置来管理布局。它一个好用的地方是嵌套的布局层次少,假设你要做一个table布局,使用linearlayout来做的话内部使用多个linearlayout来协作完成,这样整个视图对象的嵌套层次就变多了,而手机因为处理器性能不高、内存也不多,所以当嵌套层次多时手机响应的速度就会变慢。而relativelayout就是用来弥补这个不足,它不需要嵌套多个布局对象就可以完成复杂的布局。
<?xml version="1.0" encoding="utf-8"?><relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#008000" > <textview android:id="@+id/label" android:text="type here:" android:layout_width="match_parent" android:layout_height="wrap_content" /> <edittext android:id="@+id/entry" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/label" /> <button android:id="@+id/ok" android:text="ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignparentright="true" /> <button android:id="@+id/cancel" android:text="cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toleftof="@id/ok" android:layout_aligntop="@id/ok" /></relativelayout>