Android:TableLayout 表格布局用法
一、TableLayout 概述
TableLayout 表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以使一个View的对象。
当为 TableRow 对象时,可在 TableRow 下添加子控件,默认情况下,每个子控件占据一列。当为View时,该View将独占一行。
TableLayout 的行数由开发人员直接指定,即有多少个TableRow对象(或View控件),就有多少行。
TableLayout 的列数取决于含有最多子控件的 TableRow 的列数。如:第一 TableRow 含2个子控件,第二个 TableRow 含3个,第三个 TableRow 含4个,那么该 TableLayout 的列数为4。
二、TableLayout的属性(全局属性)
1.设置要隐藏的列
android:collapseColumns="1,2"
隐藏从0开始的索引列,列之间必须用逗号隔开1,2。
2.设置可收缩的列
android:shrinkColumns="1,2"
收缩从0开始的索引列,当可收缩的列太宽(内容太多时)不会被挤出屏幕,列之间用逗号隔开,如:1,2,你可以通过"*"代替收缩所有列,注意一列能同时表示收缩和拉伸。
3.设置可伸展的列
android:stretchColumns=”1,2”
拉伸从0开始的索引列,以填满剩下的多余空白空间。
三、局部属性(内部控件所用属性)
android:layout_column="1" 指定该单元格在第几列显示。
android:layout_span="2" 指定该单元格占据的列数(未指定时,为1)
四、实例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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,用于描述表中单元格的属性,包括:android:layout_column 及android: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,使用可伸展特性,并指定每个控件宽度一致,如1dip-->
<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>
<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
style="?android:attr/buttonStyleSmall"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
</TableRow>
</TableLayout>
</LinearLayout>
效果如下图: