編輯:關於 Android 程式設計
一、表格版面 TableLayout
TableLayout 以列與行的形式管理子元素,每一行通常是一個 TableRow 物件,也可以是普通的 View;TableRow 每放一個元素就是一格,總格數由格數最多的那一行決定。
我們看一個例子:
<?xml version=”1.0″ encoding=”utf-8″?> <TableLayout android:id=”@+id/TableLayout01″ android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:stretchColumns=”0″ xmlns:android=”https://schemas.android.com/apk/res/android”><TableRow android:layout_width=”fill_parent” android:layout_height=”20dip”> <TextView android:text=”色彩透明度測試” android:textSize=”18dip” android:layout_span=”2″ 合並兩列 android:layout_gravity=”center” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> </TextView>
顯示效果如下:

其中 android:stretchColumns=”0″ 的作用是讓第一格可擴展到所有可用空間。以下說明 TableLayout 的幾個重要屬性:
可用「*」代表所有格,同一格也能同時設為可收縮與可伸展。
再看一個例子:
<?xml version=”1.0″ encoding=”utf-8″?> <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:layout_column=”1″ 我是第二列 android:text=”打開…” android:padding=”3dip” /> 元素內容與邊界之間保留3dip的距離 <TextView android:text=”Ctrl-O” android:gravity=”right” android:padding=”3dip” /> </TableRow><TableRow> <TextView android:layout_column=”1″ android:text=”保存…” android:padding=”3dip” /> <TextView android:text=”Ctrl-S” android:gravity=”right” 元素本身的內容向右對齊 android:padding=”3dip” /> </TableRow><TableRow> <TextView android:layout_column=”1″ android:text=”另存為…” android:padding=”3dip” /> <TextView android:text=”Ctrl-Shift-S” android:gravity=”right” android:padding=”3dip” /> </TableRow><View android:layout_height=”2dip” android:background=”#FF909090″ /><TableRow> <TextView android:text=”X” android:padding=”3dip” /> <TextView android:text=”導入…” android:padding=”3dip” /> </TableRow><TableRow> <TextView android:text=”X” android:padding=”3dip” /> <TextView android:text=”導出…” android:padding=”3dip” /> <TextView android:text=”Ctrl-E” android:gravity=”right” android:padding=”3dip” /> </TableRow><View android:layout_height=”2dip” android:background=”#FF909090″ /><TableRow> <TextView android:layout_column=”1″ android:text=”退出” android:padding=”3dip” /> </TableRow> </TableLayout>
顯示效果如下:

程式碼中加粗處都附上了說明,大家可以留意一下。
Tip:TableRow 本身也是一種 Layout,其內部元素會水平排列;若 TableRow 的父元素不是 TableLayout,它就會表現得如同一個 LinearLayout。
以上就是 Android TableLayout 的介紹,後續將繼續補充相關資料。
Android Scroller 滾動機制完全解析
Android 的 View 本身就具備滾動能力,核心方法是 scrollTo 與 scrollBy。若想做出更順滑、可控的滾動動畫,則需要搭配 Scroller 與 c
Android ViewPager + Fragment 實作選項卡
用 ViewPager 搭配 Fragment 做選項卡,是早期 Android 開發中相當經典的一種寫法。以下逐步拆解。 效果圖: 1. 建立 TestFragme
Activity 生命週期與 onSaveInstanceState、onRestoreInstanceState 重點整理
把 Activity 生命週期與狀態保存/還原的時機一次說清楚 一般來說,當 Activity 的生命週期走到 onPause() 之後,系統緊接著就會回呼: pro
Android Log 日誌知識整理:級別、格式與過濾
Android 的 Log 是開發階段最常用也最容易被忽略的除錯工具。本文整理 Log 類別、常用級別、輸出格式與過濾方式,幫助開發者快速定位問題。 Log 的基本結構