Android布局之表格布局[通俗易懂]

Android布局之表格布局[通俗易懂]表格布局(Tablelayout) 简介:       Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。     当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。     当为View时,该View将独占一行。表格布局是以行和列的形式来对控件…

大家好,又见面了,我是你们的朋友全栈君。

表格布局(Tablelayout)

 

简介:   

         Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。

         当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。

         当为View时,该View将独占一行。

表格布局是以行和列的形式来对控件进行管理的,所以我们来说说表格布局对行和列的确定

TableLayout的行数

在开发中由我们来直接指定,就是说有多少个TableRow对象或view控件就会有多少行。

TableLayout的列数

等于含有最多子控件的TableRow的列数。如第一(行)TableRow含2个子控件,第二(行)TableRow含3个,第三(行)TableRow含4个,那么这个表格布局的列数就是4列。

 

TableLayout可设置的属性

 表格布局可以设置的属性有两种:全局属性、单元格属性。

 

全局属性(列属性):  全局属性有三个属性

   Android:stretchColumns    设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。

  Android:shrinkColumns      设置可收缩的列。(当该列子控件里的内容太多,行内显示不完的时候会向列的方向显示内容)。

  Android:collapseColumns  设置要隐藏的列。

下面就来举例说明一下:

Android:stretchColumns=”0″           第0列可伸展

Android:shrinkColumns=”1,2″         第1,2列皆可收缩

Android:collapseColumns=”1″         隐藏第一行

 

单元格属性:  单元格属性有两个属性

Android:layout_column    指定该单元格在第几列显示

Android:layout_span        指定该单元格占据的列数(如果我们在使用中没有指定,那么默认值将为1)

下面就来举例说明一下:

Android:layout_column=”1″    该控件在第1列

Android:layout_span=”2″        该控件占了2列

 

下面我们来整体运用一下表格布局里的属性(代码和效果图):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="3dip"
    >

    <!-- 第1个TableLayout,用于描述表中的列属性。第0列可伸展,第1列可收缩 ,第2列被隐藏-->
    <TextView
        android:text="第一个表格:全局设置:列属性设置"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="15sp"
        android:background="#7f00ffff"/>
    <TableLayout
        android:id="@+id/table1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0"
        android:shrinkColumns="1"
        android:collapseColumns="2"
        android:padding="3dip">
        <TableRow>
            <Button android:text="该列可以伸展"/>
            <Button android:text="该列可以收缩"/>
            <Button android:text="被隐藏了"/>
        </TableRow>

        <TableRow>
            <TextView android:text="向行方向伸展,可以伸展很长    "/>
            <TextView android:text="向列方向收缩,*****************************************************************************************可以伸缩很长"/>
        </TableRow>

    </TableLayout>

    <!-- 第2个TableLayout,用于描述表中单元格的属性,包括:android:layout_column 及android:layout_span-->
    <TextView
        android:text="第二个:单元格设置:指定单元格属性设置"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="15sp"
        android:background="#7f00ffff"/>
    <TableLayout
        android:id="@+id/table2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="3dip">
        <TableRow>
            <Button android:text="第1列"/>
            <Button android:text="第2列"/>
            <Button android:text="第3列"/>
        </TableRow>

        <TableRow>
            <TextView android:text="指定在第2列" android:layout_column="1"/>
        </TableRow>

        <TableRow>
            <TextView
                android:text="第二列和第三列!!!!!!!!!!!!"
                android:layout_column="1"
                android:layout_span="2"
                />
        </TableRow>

    </TableLayout>

    <!-- 第3个TableLayout,使用可伸展特性布局-->
    <TextView
        android:text="第三个表格:非均匀布局,控件长度根据内容伸缩"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="15sp"
        android:background="#7f00ffff"/>
    <TableLayout
        android:id="@+id/table3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*"
        android:padding="3dip"
        >
        <TableRow>
            <Button android:text="一笑山河红袖遮" ></Button>
            <Button android:text="姜泥"></Button>
            <Button android:text="两剑惊破旧山河" ></Button>
        </TableRow>
    </TableLayout>

    <!-- 第4个TableLayout,使用可伸展特性,并指定每个控件宽度一致,如1dip-->
    <TextView
        android:text="表4:均匀布局,控件宽度一致"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="15sp"
        android:background="#7f00ffff"/>
    <TableLayout
        android:id="@+id/table4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*"
        android:padding="3dip"
        >
        <TableRow>
            <Button android:text="徐凤年" android:layout_width="1dip"></Button>
            <Button android:text="温华" android:layout_width="1dip"></Button>
            <Button android:text="天不生我李淳罡" android:layout_width="1dip"></Button>
        </TableRow>
    </TableLayout>
</LinearLayout>

 

Android布局之表格布局[通俗易懂]

说完了怎么用,咱们再来说说表格布局的优点和缺点:

 

优点:

1、结构位置更简单 

2、容易上手 

3、 数据化的存放更合理。

例如,学生信息这样的表,相对简单,如果用别的布局的话就比较麻烦信息也比较杂乱。

缺点:

1、 标签结构多,代码复杂 

2、 表格布局,不利于搜索引擎抓取信息
 

 

 

这就是Android常用布局中的表格布局啦~

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146261.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • nginx负载均衡的原理简介_负载均衡原理

    nginx负载均衡的原理简介_负载均衡原理1、Nginx负载均衡的原理是什么?​客户端向反向代理发送请求,接着反向代理根据某种负载机制转发请求至目标服务器(这些服务器都运行着相同的应用),并把获得的内容返回给客户端,期中,代理请求可能根据配置被发往不同的服务器。2、Nginx负载均衡的作用是什么?​负载均衡:分摊到多个操作单元上进行执行,和它的英文名称很匹配。就是我们需要一个调度者,保证所有后端服务器都将性能充分发挥,从而保持服…

    2022年10月9日
    3
  • 小技巧两个感叹号(两个!)连用

    小技巧两个感叹号(两个!)连用

    2021年11月8日
    288
  • python2.7安装matplotlib_Matplotlib安装

    python2.7安装matplotlib_Matplotlib安装Matplotlib安装教程有很多不同的方法来安装matplotlib,最好的方法取决于你正在使用什么操作系统,你已经安装了什么,以及如何使用它。为了避免在此页面上浏览所有细节(和潜在的并发症),有几个方便的选择。安装预建的软件包大多数平台:科学的Python发行第一个选择是使用已经提供matplotlib内置的预包装的python发行版之一。Continuum.ioPython发行版(Anac…

    2022年6月29日
    27
  • Spring Boot 2.x: 定时给对象发送天气

    Spring Boot 2.x: 定时给对象发送天气使用Java写一个定时给对象发送天气的功能前言技术栈快速创建实例pom.xml文件新建接收天气api的实体天气接口封装的天气api简单演示获取天气api与发送邮件的逻辑设置发送账号信息配置appliction.properties控制层启动类效果源码地址前言不知不觉,又到了雨季,你对象是不是经常忘记带伞呢,这个时候写一个自动定时发送邮件的程序,提醒她带伞,会不会对你崇拜有加呢,当然,如果你对象是一位攻城狮,当我没讲~技术栈SpringBoot2.3.1Jdk1.8Maven快速创

    2022年7月27日
    9
  • kong 网关 修改返回数据_kong网关教程

    kong 网关 修改返回数据_kong网关教程一、简介路由用来匹配客户端向上游服务器请求的规则,也就是客户端调用的API,每个路由(Route)和一个服务(Service)相关联,一个服务可有有多个路由,我们可以对每一条路由进行细粒度的配置,可以使用正则表达式进行通用的配置。二、重要属性创建一个路由需要配置的属性,其中路径paths为必须设置,其余为可选。AttributesDescriptionname…

    2025年10月22日
    7
  • MySQL 5.7.9版本sql_mode=only_full_group_by问题

    用到GROUPBY语句查询时com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Expression#2ofSELECTl

    2022年3月29日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号