Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android TableLayout 教學:span、shrinkColumns 與 stretchColumns 實操

編輯:Android 開發實例

TableLayout 和我們在網頁上熟悉的 table 有點不同:它沒有邊框,由多個 TableRow 組成。每個 TableRow 可以有 0 個或多個儲存格,每個儲存格就是一個 View。這些 TableRow 與儲存格都不能設定 layout_width(寬度預設為 fill_parent),只有高度 layout_height 可以自訂,預設是 wrap_content。

此外還有幾個實用特性:儲存格可以留空,並以 android:layout_column 指定索引來跳過某些格子;在兩個 TableRow 之間插入一個 View 並設定高度與背景色,就能畫出一條分隔線;android:layout_span 則可合併多個儲存格。

基本範例

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow>
        <TextView android:text="column1" android:padding="3dip" />
        <TextView android:text="column2" android:padding="3dip" />
        <TextView android:text="column3" android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView android:text="column11" android:visibility="invisible" /> <!-- 這個 cell 隱形 -->
        <TextView android:text="左邊的 invisible"
            android:gravity="right" android:padding="3dip" />
        <Button android:id="@+id/go" android:text="go" android:padding="3dip" />
        <Button android:text="cancel" android:padding="3dip" />
    </TableRow>

    <View <!-- 分隔線 -->
        android:layout_height="2dip"
        android:background="#F00" />

    <TableRow>
        <TextView android:text="右邊的 cell empty" />
        <TextView android:layout_column="2"
            android:text="跳開 empty cell" android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView android:text="合併 3 個儲存格"
            android:layout_span="3"
            android:gravity="center_horizontal"
            android:background="#FFC0C0C0"
            android:textColor="#f00"
            android:padding="3dip" />
    </TableRow>
</TableLayout>

這裡可以看到幾件事:visibility 設為 invisible 的儲存格仍佔位;layout_column="2" 讓文字直接跳到第三欄、跳過右側空白的格子;而 layout_span="3" 讓單一 TextView 橫跨三欄並置中。

但少了 padding 就會出問題。如果不加 android:padding="3dip",各欄會緊緊擠在一起毫無間距,上面的 cancel 按鈕甚至幾乎被擠到看不見。這個時候就需要 android:shrinkColumns 與 android:stretchColumns。

shrinkColumns 與 stretchColumns 的規則

兩者的值都是以 0 開始的 column index,而且必須是字串,例如 "1,2,5";也可以用 "*" 代表 all columns。同一個 column 可以同時被設為 shrinkable 與 stretchable。

以程式碼設定的話會麻煩一些,需要逐一呼叫 setColumnShrinkable(int columnIndex, boolean isShrinkable) 或 setColumnStretchable(...);若要一次處理全部欄位,則用 setShrinkAllColumns() 與 setStretchAllColumns()。查詢狀態則用 isColumnShrinkable(int)、isColumnStretchable(int)、isShrinkAllColumns()、isStretchAllColumns()。

可收縮欄位的實際效果

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0"> <!-- 第一欄可收縮 -->

    <TableRow>
        <TextView android:text="column1" android:padding="3dip" />
        <TextView android:text="column2" android:padding="3dip" />
        <TextView android:text="column3" android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView android:text="column11" android:visibility="invisible" />
        <TextView android:text="左邊的 invisible"
            android:gravity="right" android:padding="3dip" />
        <Button android:id="@+id/go2" android:text="go2" android:padding="3dip" />
        <Button android:text="cancel" android:padding="3dip" />
    </TableRow>

    <View android:layout_height="2dip" android:background="#F00" />

    <TableRow>
        <TextView android:text="右邊的 cell empty" />
        <TextView android:layout_column="2" android:text="跳開 empty cell" android:padding="3dip" />
        <TextView android:text="123456789" android:padding="3dip" />
    </TableRow>
</TableLayout>

結果可以看到:第一欄為了讓第四欄完整顯示,內容被壓縮成多行換行呈現。可收縮的本質是「空間不足時,犧牲這一欄的寬度」。

可伸展欄位的實際效果

反過來說,可伸展是在其他欄位都能完整顯示之後,讓指定欄位去吃掉剩下的空間:

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1"> <!-- 第二欄可伸展 -->

    <TableRow>
        <TextView android:text="column1" android:padding="3dip" />
        <TextView android:text="column2" android:gravity="right" android:padding="3dip" />
        <TextView android:text="column3" android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView android:text="column1" android:padding="3dip" />
        <TextView android:text="column2" android:gravity="right" android:padding="3dip" />
        <TextView android:text="column3" android:padding="3dip" />
    </TableRow>
</TableLayout>

這也是為什麼表單類型的畫面幾乎都會寫 android:stretchColumns="1":讓標籤欄固定、輸入欄吃掉剩餘寬度。

動態隱藏欄位

執行期可用 TableLayout.setColumnCollapsed(int columnIndex, boolean isCollapsed) 隱藏指定欄位,並以 boolean isColumnCollapsed(int columnIndex) 查詢狀態。

實用場景:做出表單畫面

TableLayout 很適合呈現網頁表單那種「左標籤、右輸入框」的排版,官方範例如下:

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">

    <TableRow>
        <TextView
            android:text="User"
            android:textStyle="bold"
            android:gravity="right"
            android:padding="3dip" />
        <EditText android:id="@+id/username"
            android:text="username"
            android:padding="3dip"
            android:scrollHorizontally="true" />
    </TableRow>

    <TableRow>
        <TextView
            android:text="Password"
            android:textStyle="bold"
            android:gravity="right"
            android:padding="3dip" />
        <EditText android:id="@+id/password"
            android:text="password"
            android:password="true"
            android:padding="3dip"
            android:scrollHorizontally="true" />
    </TableRow>

    <TableRow android:gravity="right">
        <Button android:id="@+id/cancel" android:text="Cancel" />
        <Button android:id="@+id/login" android:text="Login" />
    </TableRow>
</TableLayout>

幾個細節值得學起來:標籤欄用 textStyle="bold" 與 gravity="right" 靠右對齊;輸入欄用 scrollHorizontally="true" 讓長文字可水平捲動而不撐破版面;最後一列用 android:gravity="right" 讓按鈕整體靠右。

使用上的提醒

TableLayout 天生是「表格」而非「通用容器」,它沒有 LinearLayout 那樣的 weight 機制,寬度分配完全靠 shrink/stretch 這兩個開關。因此適合用在欄位對齊要求高的表單或數據呈現;若只是單純的垂直堆疊,用 LinearLayout 會更直觀。另外,由於每個儲存格都是一個 View,欄位過多時層數與元件數會快速增加,記得搭配 padding 與必要時的 ViewStub 控制複雜度。

本文整理自:https://android.blog.51cto.com/268543/314262

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved