編輯:關於 Android 程式設計
TableLayout 標簽用於實現表格布局,它繼承自 LinearLayout,因此本質上仍是一種線性布局。本文將從基本概念到屬性配置,全面剖析 TableLayout 的用法。
TableLayout 以行和列的形式管理控件,每一行為一個 TableRow 對象或一個 View 控件。當元素為 TableRow 時,可在其下添加子控件,默認情況下每個子控件占據一列;當元素為 View 時,該 View 將獨占一行。
TableLayout 的行數由開發者直接指定,即有多少個 TableRow(或 View),就有多少行。而列數則等於含有最多子控件的 TableRow 的列數。例如第一個 TableRow 含 2 個子控件,第二個含 3 個,第三個含 4 個,那麼該 TableLayout 的列數就是 4。
TableLayout 的屬性分為全局屬性(列屬性)與單元格屬性兩類。
全局屬性有三個參數:
android:stretchColumns 設置可伸展的列,該列最多可占據一整行。
android:shrinkColumns 設置可收縮的列,當該列內容過多擠滿所在行時,內容會往列方向換行顯示。
android:collapseColumns 設置要隱藏的列。
示例:
android:stretchColumns="0" 表示第 0 列可伸展。
android:shrinkColumns="1,2" 表示第 1、2 列皆可收縮。
android:collapseColumns="*" 表示隱藏所有列。
說明:一列可同時具備 stretchColumns 與 shrinkColumns 屬性,此時若該列內容較多,系統會按需自動調整該行的 layout_height 以多行顯示。
單元格屬性有兩個參數:
android:layout_column 指定該單元格在第幾列顯示。
android:layout_span 指定該單元格占據的列數,未指定時默認為 1。
示例:
android:layout_column="1" 表示該控件顯示在第 1 列。
android:layout_span="2" 表示該控件占據 2 列。
說明:一個控件也可同時具備這兩個特性。
下面是一個包含多個 TableLayout 的完整布局示例,展示了列屬性設置、單元格屬性設置以及均勻與非均勻布局的應用:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="3dip" > <!-- 第1個TableLayout:全局設置,列屬性設置。第0列可伸展,第1列可收縮,第2列被隱藏 --> <TextView android:text="表1:全局設置:列屬性設置" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="15sp" android:background="#7f00ffff"/> <TableLayout android:id="@+id/table1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0" android:shrinkColumns="1" android:collapseColumns="2" android:padding="3dip"> <TableRow> <Button android:text="該列可伸展"/> <Button android:text="該列可收縮"/> <Button android:text="我被隱藏了"/> </TableRow> <TableRow> <TextView android:text="我向行方向伸展,我可以很長 "/> <TextView android:text="我向列方向收縮,我可以很深"/> </TableRow> </TableLayout> <!-- 第2個TableLayout:單元格屬性設置,包括 layout_column 與 layout_span --> <TextView android:text="表2:單元格設置:指定單元格屬性設置" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="15sp" android:background="#7f00ffff"/> <TableLayout android:id="@+id/table2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="3dip"> <TableRow> <Button android:text="第0列"/> <Button android:text="第1列"/> <Button android:text="第2列"/> </TableRow> <TableRow> <TextView android:text="我被指定在第2列" android:layout_column="2"/> </TableRow> <TableRow> <TextView android:text="我跨1到2列,不信你看!" android:layout_column="1" android:layout_span="2" /> </TableRow> </TableLayout> <!-- 第3個TableLayout:非均勻布局 --> <TextView android:text="表3:應用一,非均勻布局" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="15sp" android:background="#7f00ffff"/> <TableLayout android:id="@+id/table3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" android:padding="3dip" > <TableRow> <Button android:text="一" ></Button> <Button android:text="兩字"></Button> <Button android:text="三個字" ></Button> </TableRow> </TableLayout> <!-- 第4個TableLayout:均勻布局,指定每個控件寬度一致 --> <TextView android:text="表4:應用二,均勻布局" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="15sp" android:background="#7f00ffff"/> <TableLayout android:id="@+id/table4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" android:padding="3dip" > <TableRow> <Button android:text="一" android:layout_width="1dip"></Button> <Button android:text="兩字" android:layout_width="1dip"></Button> <Button android:text="三個字" android:layout_width="1dip"></Button> </TableRow> </TableLayout> <!-- 第5個TableLayout:均勻布局,多控件示例 --> <TextView android:text="表5:應用三,均勻布局" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="15sp" android:background="#7f00ffff"/> <TableLayout android:id="@+id/table5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" android:padding="6dip" > <TableRow> <Button android:text="一" android:layout_width="1dip"></Button> <Button android:text="兩字" android:layout_width="1dip"></Button> <Button android:text="三個字" android:layout_width="1dip"></Button> <Button android:text="四個個字" android:layout_width="1dip"></Button> <Button android:layout_width="1dip" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" /> </TableRow> </TableLayout> </LinearLayout>
以上就是關於 Android TableLayout 表格布局的完整介紹,希望對大家的學習有所幫助。
自定義 View 實戰(二):用複合控件封裝一個可復用的 TopBar
複合控件:把「一組控件」變成「一個控件」 自定義 View 的第二條路徑是創建複合控件。它的核心思路並不複雜:挑一個合適的 ViewGroup 作為父容器,把若干功能明確
Android Studio 除錯技巧大全:從單步執行到條件斷點
Android Studio 已是 Android 開發的主力工具,而除錯、定位、修掉 bug 是日常。所謂工欲善其事必先利其器,這篇把 IDE 的除錯功能完整梳理一遍。
啟動頁 SplashActivity 怎麼做:元素設計、異步加載與跳轉動畫
比較成熟的應用,幾乎都會在進入主界面前放一個啟動頁。它或簡單或華麗,或樸素或炫目,用處不同,風格自然也不同。先看幾個主流應用的啟動頁找找感覺。 一、貨比三家 以 QQ、
Android CTS 測試入門:從環境準備到跑單一測試案例
一、什麼是 CTS CTS 是 Compatibility Test Suite(相容性測試套件)的縮寫。Google 定義了一套相容性規範(Compatible Def