TableLayout(表格布局)
TbaleLayout的一些属性;
xml 相关用法 说明
android:collapseColumns setColumnsCollapsed(in,boolean) 设置需要隐藏的列的序号,多个用逗号隔
android:shrinkColumns setShrinkColumns(boolean) 设置需要收缩的列的序号,多个用逗号隔
android:stretchColumns setStretchAllColumns(boolean) 设置需要伸张的列的序号,多个用逗号隔
说明:收缩和伸张的属性可以同时使用,stretchColumns是将父控件填满
属性值是从0开始,0代表第一列
行数和列数的确定:是通过TableRow以及其他组件控制表格的行数和列数,具体如下:
TableLayout的行数有开发人员制定,即有多少个TableRow对象(或view控件),就有多少行
列的宽度:又该列中最宽的单元格决定,整个表格布局的宽度取决于父容器的宽度(默认是占满父容器本身) 如第一个TableRow含2个控件,第二个TableRow含3个控件,那么该TableRow的列数为3
单元格属性,2个参数
android:layout_column 指定单元格在第几列显示
android:layout_span 指定单元格占的列数(未指定时 为1)
且一个空间也可以同时具备这两个特性
android:collapse=“1”因此该TableLayout里的TableRow的列1,即第二列(从0开始计算)
示例:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
</TableRow>
<TableRow >
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button2" />
</TableRow>
<TableRow >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button3" />
</TableRow>
<TableRow >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button4" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow >
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button6" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button8" />
</TableRow>
</TableLayout>
效果图:
示例2
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1,2" >
<TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_column="0" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:layout_column="1" />
</TableRow>
<TableRow>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:layout_column="1" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"
android:layout_column="1" />
</TableRow>
<TableRow>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5"
android:layout_column="2" />
</TableRow>
</TableLayout>
效果图;