表格布局TableLayout
表格布局(TableLayout),是通过表格来管理内部的组件排列,表格管理器通过设定行和列来划分区域,布局管理器中的列可以设置为隐藏或者伸展,这些都是他们的特性,TableLayout布局是有行组成的TableRow,每个TableRow里可以放置所需要的组件。
1.表格布局管理器基本语法
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
属性列表
>
<TableRow `属性列表` >添加组件</TableRow>
可以有多个<TableRow >
</TableLayout >
2.基本属性
1.collapseColumns:设置需要被隐藏的列的序号(序列号从0开始),多个列之间用,分隔------------隐藏列
2.android:shrinkColumns:设置允许被收缩的列的列序号------- ------------------------------------------ ----收缩列
3.android:stretchColumns:设置运行被拉伸的列的列序号------------------------------------------------------拉伸列
4.android:layout_column=“2”:表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的!-------跳格
5.android:layout_span=“4”:表示合并4个单元 格,也就说这个组件占4个单元格---------------------------合并单元格
注:前三个起始数字都是0,可用逗号隔开,所有用 “*”
3.如何确定行数和列数
- 如果我们直接往TableLayout 中添加控件,那么这个组件将占满一行
- 如果想一行有多个组件,添加TableRow容器,把组件都放在TableRow容器中
- TableRow中的组件个数决定此行有多少列,宽度由最大单元格决定
- Tablerow的layout_width属性,默认是match_parent的,我们自己设置成其他的值也不会生效!!! 但是layout_height默认是wrap_content的,我们却可以自己设置大小!
- 整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
- 行数=Tablerow的个数+单独的组件个数。多少列则是看TableRow中 的组件个数,组件最多的就是TableLayout的列数
例1
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:collapseColumns="0,2" `隐藏1,3,行`
>
<TableRow >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1.1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1.2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1.3"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1.4"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1.5"
/>
</TableRow>
<TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2.1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2.2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2.3"
/>
</TableRow>
</TableLayout>
效果:
没有隐藏 android:collapseColumns=“0,2” 隐藏1,3,行
有隐藏 android:collapseColumns=“0,2” 隐藏1,3,行
例2
设置四行四列,
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:stretchColumns="0,3" `没有此行,只能垂直居中,水平效果显示不出来`
android:gravity="center_vertical"
>
<TableRow >
<TextView /> `占位`
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"
/>
<TextView /> `占位`
</TableRow>
<TableRow >
<TextView />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"
/>
<TextView />
</TableRow>
<TableRow >
<TextView />
<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>
<TextView />
</TableLayout>
例3
<TableLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1,2,3"
>
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_column="0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:layout_column="2"
/>
</TableRow>
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"
android:layout_column="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button4"
/>
</TableRow>
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button5"
android:layout_column="2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button6"
/>
</TableRow>
### 效果 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201022213122476.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NzZG5iaWFu,size_16,color_FFFFFF,t_70#pic_center)