Android五大布局(二)——RelativeLayout、TableLayout 和AbsoulteLayout
在 Android五大布局(一)http://dtfy56924.iteye.com/blog/1775551 中已经描述了LinearLayout(线性布局)、FrameLayout(单帧布局)两种布局结构
下面说下RelativeLayout、TableLayout和AbsoulteLayout
Android 众多的布局属性详解
http://www.open-open.com/lib/view/open1328686184311.html
参考与:
http://www.eoeandroid.com/thread-249585-1-1.html
一、RelativeLayout:
RelativeLayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below, android:layout_above等。子元素就通过这些属性和各自的ID配合指定位置关系。注意在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。
RelativeLayout里常用的位置属性如下:
android:layout_toLeftOf —— 该组件位于引用组件的左方
android:layout_toRightOf —— 该组件位于引用组件的右方
android:layout_above —— 该组件位于引用组件的上方
android:layout_below —— 该组件位于引用组件的下方
android:layout_alignParentLeft —— 该组件是否对齐父组件的左端
android:layout_alignParentRight —— 该组件是否齐其父组件的右端
android:layout_alignParentTop —— 该组件是否对齐父组件的顶部
android:layout_alignParentBottom —— 该组件是否对齐父组件的底部
android:layout_centerInParent —— 该组件是否相对于父组件居中
android:layout_centerHorizontal —— 该组件是否横向居中
android:layout_centerVertical —— 该组件是否垂直居中
RelativeLayout是Android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。下面示例就展示这么一个情况,第一个文本框与父组件的底部对齐,第二个文本框位于第一个文本框的上方,并且第三个文本框位于第二个文本框的左方。
例子效果图:
<?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="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text_01" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="#ffffffff" android:gravity="center" android:text="1" /> <TextView android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:layout_above="@id/text_01" android:layout_centerHorizontal="true" android:background="#ff654321" android:gravity="center" android:text="2" /> <TextView android:id="@+id/text_03" android:layout_width="50dp" android:layout_height="50dp" android:layout_above="@id/text_01" android:layout_toLeftOf="@id/text_02" android:background="#fffedcba" android:gravity="center" android:text="3" /> </RelativeLayout>

<?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:orientation="vertical" > <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>

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="50dp" android:layout_height="50dp" android:layout_x="50dp" android:layout_y="50dp" android:background="#ffffffff" android:gravity="center" android:text="1" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:layout_x="25dp" android:layout_y="25dp" android:background="#ff654321" android:gravity="center" android:text="2" /> <TextView android:layout_width="50dp" android:layout_height="50dp" android:layout_x="125dp" android:layout_y="125dp" android:background="#fffedcba" android:gravity="center" android:text="3" /> </AbsoluteLayout>