表格佈局(TableLayout)類以行和列的形式管理控件,每一行爲一個TableRow對象,也可以作爲一個View對象;當爲View對象時,該View對象將跨越該行的所有列。在TableRow中可以添加子控件,每添加一個子控件即爲一列。
TableLayout佈局中並不會爲每一行、每一列或每個單元格繪製邊框,每一行可以有0個或多個單元格,每個單元格爲一個View對象。TableLayout中可以有空的單元格,單元格也可以像HTML中那樣跨越多個列。
在TableLayout佈局中,一個列的寬度由該列中最寬的那個單元格指定,而表格的寬度是由父容器指定的。在TableLayout中,可以爲列設置三種屬性:
- Shrinkable:如果一個列被標識爲Shrinkable,則該列的寬度可以進行收縮,以使表格能夠適應其父容器的大小。
- Stretchable:如果一個列被標識爲Stretchable,則該列的寬度可以進行拉伸,以使填滿表格中的空閒空間。
- Collapsed:如果一個列被標識爲Collapsed,則該列會被隱藏。
注意:一個列可以同時具有Shrinkable屬性和Stretchable屬性,在這種情況下,該列的寬度將任意拉伸或收縮以適應父容器。
TableLayout繼承自LinearLayout類,除了繼承來自父類的屬性和方法,TableLayout類中還包含表格佈局所特有的屬性和方法,如下表:
屬性名稱 | 對應方法 | 描述 |
android:collapseColumns | setColumnCollapsed(int,boolean) | 設置指定列號的列屬性爲Collapsed |
android:shrinkColumns | setShrinkAllColumns(boolean) | 設置指定列號的列屬性爲Shrinkable |
android:stretchColumns | setStretchAllColumns(boolean) | 設置指定列號的列屬性爲Stretchable |
注意:TableLayout中所謂的列序號是從0開始計算的。setShrinkAllColumns和setStretchAllColumns實現的功能是將表格中的所有列設置爲Shrinkable或Stretchable。
下面來看一下效果:
其中Main.xml代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- android:id="@+id/LinearLayout01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="@drawable/android"
- android:gravity="bottom">
- <!-- 第一行 -->
- <TableLayout
- android:id="@+id/TableLayout01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#FFFFFF"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:text="我是單獨的一行"
- android:id="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:background="#fd8d8d"
- android:textColor="#000000"
- android:layout_margin="4px"
- >
- </TextView>
- </TableLayout>
- <TableLayout
- android:id="@+id/TableLayout02"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#FFFFFF"
- android:stretchColumns="0"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- android:stretchColumns="0" 設置0號列爲可伸展的列,當有多個列可伸展時用逗號隔開 -->
- <!-- 第二行 -->
- <TableRow
- android:id="@+id/TableRow01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <!-- 第一列 -->
- <TextView
- android:text="我是被拉上的一列"
- android:id="@+id/TextView02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:background="#9cfda3"
- android:textColor="#000000"
- android:layout_margin="4px">
- </TextView>
- <!-- 第二列 -->
- <TextView
- android:text="我的內容少"
- android:id="@+id/TextView03"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:background="#8d9dfd"
- android:textColor="#000000"
- android:layout_margin="4px"
- >
- </TextView>
- </TableRow>
- </TableLayout>
- <TableLayout
- android:id="@+id/TableLayout03"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#FFFFFF"
- android:collapseColumns="1"
- android:shrinkColumns="0"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- android:collapseColumns="1" 隱藏編號爲1的列,若有多個列要隱藏,則用逗號隔開,如0,2 -->
- <!-- android:shrinkColumns="0"
- 設置0號列爲可收縮的列,可收縮的列會縱向擴展
- 若有多個列要收縮,則用逗號隔開,如0,2 -->
- <!-- 第三行 -->
- <TableRow
- android:id="@+id/TableRow02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <!-- 第一列 -->
- <TextView
- android:text="我的被收縮的一列被收縮的一列"
- android:id="@+id/TextView04"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:background="#9cfda3"
- android:textColor="#000000"
- android:layout_margin="4px">
- </TextView>
- <!-- 第二列,被設置爲隱藏了 -->
- <TextView
- android:text="我的內容少"
- android:id="@+id/TextView05"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:background="#8d9dfd"
- android:textColor="#000000"
- android:layout_margin="4px"
- >
- </TextView>
- <!-- 第三列 -->
- <TextView
- android:text="我的內容比較長比較長比較長"
- android:id="@+id/TextView06"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:background="#fd8d8d"
- android:textColor="#000000"
- android:layout_margin="4px"
- >
- </TextView>
- </TableRow>
- </TableLayout>
- </LinearLayout>
Activity代碼爲:
- package com.sunchis;
- import android.app.Activity;
- import android.os.Bundle;
- public class Android extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main); //設置屏幕
- }
- }