TableLayout可設置的屬性包括全局屬性及單元格屬性。佈局
一、全局屬性也即列屬性,有如下3個參數:url
android:stretchColumns
spa 設置可伸展的列。該列能夠向行方向伸展,最多可佔據一整行。
android:shrinkColumns
3d 設置可收縮的列。當該列子控件的內容太多,已經擠滿所在行,那麼該子控件的內容將往列方向顯示。
android:collapseColumns 設置要隱藏的列。orm
示例:xml
android:stretchColumns="0"
htm 第0列可伸展
android:shrinkColumns="1,2"
對象 第1,2列皆可收縮
android:collapseColumns="*"
隱藏全部行 說明:列能夠同時具有stretchColumns及shrinkColumns屬性,若此,那麼當該列的內容N多時,將「多行」顯示其內容。(這裏不是真正的多行,而是系統根據須要自動調節該行的layout_height)
二、單元格屬性,有如下2個參數:
android:layout_column
指定該單元格在第幾列顯示 android:layout_span
指定該單元格佔據的列數(未指定時,爲1)
示例:
android:layout_column="1"
該控件顯示在第1列 android:layout_span="2"
該控件佔據2列 說明:一個控件也能夠同時具有這兩個特性。
4、一個包含4個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"
android:padding="3dip"
>
<!-- 第1個TableLayout,用於描述表中的列屬性。第0列可伸展,第1列可收縮,第2列被隱藏-->
<TextView
android:text="表1:全局設置:列屬性設置"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
android:shrinkColumns="1"
android:collapseColumns="2"
android:padding="3dip">
<TableRow>
<Button android:text="該列可伸展"/>
<Button android:text="該列可收縮"/>
<Button android:text="我被隱藏了"/>
</TableRow>
<TableRow>
<TextView android:text="我向行方向伸展,我能夠很長 "/>
<TextView android:text="我向列方向收縮,我能夠很深"/>
</TableRow>
</TableLayout>
<!-- 第2個TableLayout,用於描述表中單元格的屬性,包括:android:layout_column 及android:layout_span-->
<TextView
android:text="表2:單元格設置:指定單元格屬性設置"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="3dip">
<TableRow>
<Button android:text="第0列"/>
<Button android:text="第1列"/>
<Button android:text="第2列"/>
</TableRow>
<TableRow>
<TextView android:text="我被指定在第1列" android:layout_column="1"/>
</TableRow>
<TableRow>
<TextView
android:text="我跨1到2列,不信你看!"
android:layout_column="1"
android:layout_span="2"
/>
</TableRow>
</TableLayout>
<!-- 第3個TableLayout,使用可伸展特性佈局-->
<TextView
android:text="表3:應用一,非均勻佈局"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:padding="3dip"
>
<TableRow>
<Button android:text="一" ></Button>
<Button android:text="兩字"></Button>
<Button android:text="三個字" ></Button>
</TableRow>
</TableLayout>
<!-- 第4個TableLayout,使用可伸展特性,並指定每一個控件寬度一致,如1dip-->
<TextView
android:text="表4:應用二,均勻佈局"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:padding="3dip"
>
<TableRow>
<Button android:text="一" android:layout_width="1dip"></Button>
<Button android:text="兩字" android:layout_width="1dip"></Button>
<Button android:text="三個字" android:layout_width="1dip"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
說明:第4個TableLayout裏的均勻佈局的均勻效果是有限的。其有限性體如今,當該行有N列,則每列的控件內容不能多於1/N。運行效果圖:(如圖1)
圖1 TableLayout運行結果圖