表格版面配置模型以行列的形式管理子控制項,每一行為一個TableRow的對象,當然也可以是一個View的對象。TableRow可以添加子控制項,每添加一個為一列。
TableLayout屬性:
android:collapseColumns:將TableLayout裡面指定的列隱藏,若有多列需要隱藏,請用逗號將需要隱藏的列序號隔開。
android:stretchColumns:設定指定的列為可伸展的列,以填滿剩下的多餘空白空間,若有多列需要設定為可伸展,請用逗號將需要伸展的列序號隔開。
android:shrinkColumns:設定指定的列為可收縮的列。當可收縮的列太寬(內容過多)不會被擠出螢幕。當需要設定多列為可收縮時,將列序號用逗號隔開。
列元素(Button)屬性:(奇怪的是button 裡面沒有android:layout_column 和android:layout_span兩個屬性,寫進去無反應,還不知道為什麼)
android:layout_colum:設定該控制項在TableRow中指定的列。
android:layout_span:設定該控制項所跨越的列數。
圖片:
代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".AndroidTableLayoutActivity" > <!-- 定義第一個表格,指定第2列允許收縮,第3列允許展開 --> <TableLayout android:id="@+id/tablelayout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:shrinkColumns="1" android:stretchColumns="2" > <!-- 直接添加按鈕,自己佔用一行 --> <Button android:id="@+id/btn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獨自一行" > </Button> <TableRow> <Button android:id="@+id/btn02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通" > </Button> <Button android:id="@+id/btn03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="允許被收縮允許被收縮允許被收縮允許被收縮" > </Button> <Button android:id="@+id/btn04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="允許被展開" > </Button> </TableRow> </TableLayout> <!-- 定義第2個表格,指定第2列隱藏 --> <TableLayout android:id="@+id/tablelayout02" android:layout_width="match_parent" android:layout_height="wrap_content" android:collapseColumns="1" > <TableRow> <Button android:id="@+id/btn05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通" > </Button> <Button android:id="@+id/btn06" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="被隱藏列" > </Button> <Button android:id="@+id/btn07" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="允許被展開" > </Button> </TableRow> </TableLayout> <!-- 定義第3個表格,指定第2列填滿空白--> <TableLayout android:id="@+id/tablelayout03" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1" > <TableRow> <Button android:id="@+id/btn08" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通" > </Button> <Button android:id="@+id/btn09" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="填滿剩餘空白" > </Button> </TableRow> </TableLayout> <!-- 定義第3個表格,指定第2列橫跨2列--> <TableLayout android:id="@+id/tablelayout04" android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow> <Button android:id="@+id/btn10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通" > </Button> <Button android:id="@+id/btn11" android:layout_column="2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="填滿剩餘空白" > </Button> </TableRow> </TableLayout> </LinearLayout>
希望本文所述對大家學習Android軟體編程有所協助。