Android-04-表格布局TableLayout

表格布局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.如何确定行数和列数

  1. 如果我们直接往TableLayout 中添加控件,那么这个组件将占满一行
  2. 如果想一行有多个组件,添加TableRow容器,把组件都放在TableRow容器中
  3. TableRow中的组件个数决定此行有多少列,宽度由最大单元格决定
  4. Tablerow的layout_width属性,默认是match_parent的,我们自己设置成其他的值也不会生效!!! 但是layout_height默认是wrap_content的,我们却可以自己设置大小!
  5. 整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
  6. 行数=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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值