編輯:關於 Android 程式設計
TableLayout 是 Android 裡做表格與表單的首選,但它有幾個與網頁 table 截然不同的行為,加上收縮/伸展機制一開始很容易搞錯,導致元件擠成一團。
一、它沒有框線。這點與網頁上的 table 不同,TableLayout 預設不畫任何邊框,分隔線得自己加。
二、它由多個 TableRow 組成。每個 TableRow 可含 0 個或多個單元格,每個單元格就是一個 View。表格結構與單元格都不能設定 layout_width——寬度固定為 fill_parent,只有 layout_height 可以自訂,預設是 wrap_content。
三、單元格可以是空的。透過 android:layout_column 指定索引,就能跳過某些格子,讓後面的內容往前補位。
四、合併靠 layout_span。而分隔線的做法很單純:在兩個 TableRow 之間插入一個 View,設定高度與背景色即可。
<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" />
<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>
這段範例示範了四件事:invisible 的格子仍佔位、layout_column 跳格、插入 View 當分隔線、以及 layout_span 合併欄位。
如果拿掉 android:padding="3dip",會發現各欄內容緊貼成一團,沒有任何間隔——那顆 cancel 按鈕甚至被擠到幾乎看不見。這時就需要收縮與伸展機制上場:
android:shrinkColumns:指定可收縮的欄android:stretchColumns:指定可伸展的欄兩個屬性的值都是以 0 起算的索引,但必須是字串,例如 "1,2,5"。也可以用 "*" 代表全部欄位。同一欄可以同時被設為可收縮與可伸展。
以 Java 設定的話:
tableLayout.setColumnShrinkable(0, true); // 逐欄設定,較繁瑣 tableLayout.setShrinkAllColumns(true); // 一次設定全部
查詢目前狀態則用 isColumnShrinkable(int)、isColumnStretchable(int),或 isShrinkAllColumns()。
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0"> <!-- 第一欄可收縮 -->
...
加上這行之後,第一欄為了讓第四欄的內容完整顯示,會主動收窄並把文字折成數行。這是處理「某欄內容過長會擠壞其他欄」的正確手段。
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"> <!-- 第二欄可伸展 -->
...
當其他欄的內容都能完整顯示後,被設為可伸展的那一欄就會佔走剩餘的空間。這正是表單排版最需要的行為:標籤欄固定寬度,輸入欄吃掉剩下的。
執行期若要隱藏某欄,用 setColumnCollapsed(int columnIndex, boolean isCollapsed);查詢則用 isColumnCollapsed(int)。
官方範例示範了 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="使用者"
android:textStyle="bold"
android:gravity="right" android:padding="3dip" />
<EditText android:id="@+id/username"
android:padding="3dip"
android:scrollHorizontally="true" />
</TableRow>
<TableRow>
<TextView android:text="密碼"
android:textStyle="bold"
android:gravity="right" android:padding="3dip" />
<EditText android:id="@+id/password"
android:password="true"
android:padding="3dip"
android:scrollHorizontally="true" />
</TableRow>
<TableRow android:gravity="right">
<Button android:id="@+id/cancel" android:text="取消" />
<Button android:id="@+id/login" android:text="登入" />
</TableRow>
</TableLayout>
這裡 stretchColumns="1" 是關鍵:第二欄(輸入框)會自動撐滿剩餘寬度,而第一欄的標籤則靠 gravity="right" 靠右對齊,視覺上形成整齊的標籤欄。最後一列用 gravity="right" 讓按鈕靠右排列,這是 TableRow 層級的對齊設定,與單一格子的 gravity 不同。
整體說來,TableLayout 的學習曲線幾乎全在 shrink 與 stretch 這兩個屬性上。把它們弄懂,剩下的都是直覺操作。
Xamarin.Android 實作:城市天氣預報應用
專案目標 這個範例主要用來驗證 C# 的網路請求能否在 Android 環境中正常使用。實作內容是一個簡單的城市天氣預報應用,使用者可選擇省份與城市,按下查詢後取得天氣資
Activity 生命週期(四):暫停與恢復的時機和注意事項
在多數正常的應用場景中,前台 Activity 偶爾會被其他可視元件遮擋而進入暫停狀態。最典型的例子是彈出一個半透明的 Activity(例如對話框樣式的頁面):此時原
我的選單我做主:用 onKeyDown 搭配 PopupWindow 打造自訂選單
Android SDK 本身提供了建立選單的預設機制。這種機制在功能上相當完整,但外觀實在有點「土」。一個畫面絢麗的程式
Android 動畫系統之屬性動畫(Property Animation)完全解析
為什麼需要屬性動畫 Android 3.0 引入的屬性動畫系統,是對早期 View Animation(補間動畫)體系的一次徹底升級,主要體現在四個方面: 彌補了 Vi