表格布局规则:
1:若直接往TableLayout中添加组件,那么这个组件将占满一行
2:若想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都放到里面
tablerow中的组件个数就决定了该行有多少列
列的宽度由该列中最宽的单元格决定
3:整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
常用的属性:
1:android:collapseColumns:设置需要被隐藏的列的序号
2:android:shrinkColumns:设置允许被收缩的列的列序号
3:android:stretchColumns:设置运行被拉伸的列的列序号
4.TableLayout的局部属性(内部控件所用属性)
android:layout_column=”1” 该控件显示在第1列
android:layout_span=”2” 该控件占据两列
隐藏第一列和第三列(从零开始)
<TableLayout
android:id="@+id/ws1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:collapseColumns="0,2" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第四块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第五块" />
</TableRow>
</TableLayout>
设置第二列为可拉伸列,让该列填满这一行所有的剩余空间
<TableLayout
android:id="@+id/ws1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三块" />
</TableRow>
</TableLayout>
设置第二列为可收缩列
<TableLayout
android:id="@+id/ws1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三块" />
</TableRow>
</TableLayout>
指定列的时候如果组件前没有其他空间 将显示空白
<TableLayout
android:id="@+id/ws1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="在tablerow外独占一行" />
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="指定列" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三块" />
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第四块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第五块" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="第六块" />
</TableRow>
</TableLayout>