Table Layout 表格佈局
TableLayout 是一個類似表格的排版方式,使用 TableRow 將內容分行,TableLayout 主要的功能有下面這幾個項目:
android:stretchColumns:將指定的欄位填滿剩餘的空間,可以用*代表是全部的欄位
android:shrinkColumns:將指定的欄位縮小空間,可以用*代表是全部的欄位
android:collapseColumns:將指定的欄位進行刪除
上面這些設定有一個地方是需要值得注意的欄位的編號是從0開始計算的,下面是 TableRow 中的功能:
android:layout_span:合併欄位的格數
android:layout_column:指定欄位的編號
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:shrinkColumns="*"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="3"
android:background="#b0b0b0"
android:padding="18dp"
android:text="Row 1"
android:textColor="#000"
android:textSize="18dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:background="#dcdcdc"
android:gravity="center"
android:padding="20dp"
android:text="Row 2 column 1"
android:textColor="#000000" />
<TextView
android:layout_weight="1"
android:background="#d3d3d3"
android:gravity="center"
android:padding="20dp"
android:text="Row 2 column 2"
android:textColor="#000000" />
<TextView
android:layout_weight="1"
android:background="#cac9c9"
android:gravity="center"
android:padding="20dp"
android:text="Row 2 column 3"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_weight="1"
android:background="#b0b0b0"
android:gravity="center"
android:padding="20dp"
android:layout_span="2"
android:text="Row 3 column 1"
android:textColor="#000000" />
<TextView
android:layout_weight="1"
android:background="#a09f9f"
android:gravity="center"
android:padding="20dp"
android:text="Row 3 column2"
android:textColor="#000000" />
</TableRow>
</TableLayout>