TableLayout的行數由開發人員直接指定,即有多少個TableRow對象(或View控制項),就有多少行。
TableLayout的列數等於含有最多子控制項的TableRow的列數。如第一TableRow含2個子控制項,第二個TableRow含3個,第三個TableRow含4個,那麼該TableLayout的列數為4.
TableLayout可設定的屬性包括全域屬性及儲存格屬性。
1、全域屬性也即列屬性,有以下3個參數:
android:stretchColumns 設定可伸展的列。該列可以向行方向伸展,最多可佔據一整行。
android:shrinkColumns 設定可收縮的列。當該列子控制項的內容太多,已經擠滿所在行,那麼該子控制項的內容將往列方向顯示。
android:collapseColumns 設定要隱藏的列。
樣本:
android:stretchColumns="0" 第0列可伸展
android:shrinkColumns="1,2" 第1,2列皆可收縮
android:collapseColumns="*" 隱藏所有行
說明:列可以同時具備stretchColumns及shrinkColumns屬性,若此,那麼當該列的內容N多時,將“多行”顯示其內容。(這裡不是真正的多行,而是系統根據需要自動調節該行的layout_height)
2、儲存格屬性,有以下2個參數:
android:layout_column 指定該儲存格在第幾列顯示
android:layout_span 指定該儲存格佔據的列數(未指定時,為1)
樣本:
android:layout_column="1" 該控制項顯示在第1列
android:layout_span="2" 該控制項佔據2列
說明:一個控制項也可以同時具備這兩個特性。
TableLayout是一個使用複雜的布局,最簡單的用法就僅僅是拖拉控制項做出個介面,但實際上,會經常在代碼裡使用TableLayout,例如做出表格的效果。本文主要介紹TableLayout的基本使用方法。
<?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"
>
<TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_column="0"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button2"
android:layout_column="1"/>
</TableRow>
</TableLayout>
-
1 android:stretchColumns = "指定的列" ------->以第0行為序,盡量把指定的列填充空白部分
設定stretchColumns=0,則結果如,第0列被盡量填充
設定stretchColumns=1,則結果如,第1列被盡量填充
2 android:shrinkColumns = "指定的列":以第0行為序,自動延伸指定的列填充可用部分
當LayoutRow裡面的控制項還沒有布滿布局時,shrinkColumns不起作用,如
當LayoutRow裡面的控制項還沒有布滿布局時(改寫一下第三個Button的內容)
3 android:collapseColumns = “指定的列” 第0行為序,隱藏指定的列>
android:collapseColumns該屬性為空白時,如:
android:collapseColumns=“0” 意思是把第0列去掉,如: