布局篇之TableLayout:
一 功能:
(1.1)TableLayout表格布局模型以列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View对象 。
(1.2)想象一下整个布局是一个大的表格,有很多行和列,很多的单元格,子元素都被放在一个一个的单元格中,单元格不能跨列,但可以空,列可以设置为可伸展的,从而适应整个屏幕,但在实际中并不会显示出这些“行“和”列"的线。一个TableLayout会拥有很多的TableRow(行), 每一行又会有Column来定义列。
二 属性:
(2.1)TableLayout表格布局的属性:
android:collapseColumns="1,2" 隐藏从0开始的索引列,列直接必须用逗号隔开:1,2,5
android:shrinkColumns="1,2" 收缩从0开始的索引列,当可收缩的列太宽(内容过多)不会被挤出屏幕,列直接必须用逗号隔开:1,2,5,你可以通过”*“代替收缩所有列,注 意一列能同时表示收缩和拉伸。
android:stretchColumns="1,2" 拉伸从0开始的索引列,以填满剩下的多余的空白控件,列直接必须用逗号隔开:1,2,5,你可以通过”*“代替收缩所有列,注意一列能同时表示收 缩和拉伸。
(2.2)TableLayout的局部属性(内部控件所使用属性)
android:layout_column = "1" 该控件显示在第1列
android:layout_span = "2" 该控件占据2列
三:练习
(3.1)android:collapseColumns="1,2" 隐藏从0开始的索引列,列直接必须用逗号隔开:1,2,5
如图所示,此TableLayout有一行,有四列,形成了四个单元格,当然了这四个单元格不会显示出来,在这四个单元格中添加了四个Button
XML代码
<?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">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
</TableRow>
</TableLayout>
下面,我们使用android:collapseColumns让第0列和第2列隐藏