表格布局的概念
表格布局(TableLayout)是以表格形式排列控件的,通過行和列將界面劃分為多個單元格拣帽,每個單元格都可以添加控件扩氢。
表格布局需要和TableRow配合使用,每一行都由TableRow對象組成痰驱,因此TableRow的數(shù)量決定表格的行數(shù)证芭。
而表格的列數(shù)是由包含最多控件的TableRow決定的,例如第1個TableRow有兩個控件担映,第2個TableRow有三個控件废士,則表格列數(shù)為3。
基本屬性
1 TableLayout屬性
屬性 | 含義 |
---|---|
android:layout_stretchColumns | 設(shè)置被拉伸的列蝇完,從0開始 |
android:layout_shrinkColumns | 設(shè)置被收縮的列官硝,從0開始 |
android:layout_collapsecolumns | 設(shè)置該列被隱藏,從0開始 |
例如:設(shè)置一個兩行三列的表格短蜕,第三列為拉伸列氢架,當(dāng)三列不能排完一行時,第三列進(jìn)行拉伸忿危。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="2"
>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="2"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="3"/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="4"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="5"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="6"
/>
</TableRow>
</TableLayout>
2 TableLayout控件屬性
屬性 | 含義 |
---|---|
android:layout_column | 設(shè)置單元格顯示位置达箍,從0開始 |
android:layout_span | 設(shè)置單元格所占列數(shù) |
例如:第五個按鈕占兩列,第六個按鈕處于第二列
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="2"
>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="2"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="3"/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="4"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="5"
android:layout_span="2"
/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="6"
android:layout_column="1"
/>
</TableRow>
</TableLayout>