浅谈Android五大布局(二)——RelativeLayout和TableLayout

 

 在浅谈Android五大布局(一)中已经描述了LinearLayout(线性布局)、FrameLayout(单帧布局)和AbsoulteLayout(绝对布局)三种布局结构,剩下的两种布局RelativeLayout(相对布局)和TableLayout(表格布局)相对以前布局结构稍显复杂一点,因此这里另起篇幅进行介绍。android

RelativeLayout:布局

  RelativeLayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below, android:layout_above等。子元素就经过这些属性和各自的ID配合指定位置关系。注意在指定位置关系时,引用的ID必须在引用以前,先被定义,不然将出现异常。spa

  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五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。下面示例就展现这么一个状况,第一个文本框与父组件的底部对齐,第二个文本框位于第一个文本框的上方,而且第三个文本框位于第二个文本框的左方。code

RelativeLayout
<?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>

TableLayout:xml

  TableLayout顾名思义,此布局为表格布局,适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就表明TableLayout中的一行。utf-8

  TableRow是LinearLayout的子类,它的android:orientation属性值恒为horizontal,而且它的android:layout_width和android:layout_height属性值恒为MATCH_PARENT和WRAP_CONTENT。因此它的子元素都是横向排列,而且宽高一致的。这样的设计使得每一个TableRow里的子元素都至关于表格中的单元格同样。在TableRow中,单元格能够为空,可是不能跨列。it

  下面示例演示了一个TableLayout的布局结构,其中第二行只有两个单元格,而其他行都是三个单元格。io

TableLayout
<?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>
相关文章
相关标签/搜索