详解Android TableLayout表格布局

 更新时间:2016年02月24日 09:03:15   作者:gisoracle  
表格布局的标签是TableLayout,TableLayout继承了LinearLayout。所以它依然是一个线性布局,通过本文给大家介绍Android TableLayout表格布局,感兴趣的朋友一起学习吧

表格布局的标签是TableLayout,TableLayout继承了LinearLayout。所以它依然是一个线性布局。

前言:

1、TableLayout简介

2、TableLayout行列数的确定

3、TableLayout可设置的属性详解

4、一个包含4个TableLayout布局的实例及效果图

<?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="20sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="1dip">
<TableRow>
<Button android:text="0"/>
<Button android:text="1"/>
<Button android:text="2"/>
<Button android:text="+"/>
<Button android:text="="/>
</TableRow>
<TableRow>
<Button android:text="3"/>
<Button android:text="4"/>
<Button android:text="5"/>
<Button android:text="-"/>
<Button android:text="*"/>
</TableRow>
<TableRow>
<Button android:text="6"/>
<Button android:text="7"/>
<Button android:text="8"/>
<Button android:text="9"/>
<Button android:text="/"/>
</TableRow>
</TableLayout>
</LinearLayout>

一、Tablelayout简介

Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。
当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。
当为View时,该View将独占一行。

二、TableLayout行列数的确定

TableLayout的行数由开发人员直接指定,即有多少个TableRow对象(或View控件),就有多少行。

TableLayout的列数等于含有最多子控件的TableRow的列数。如第一TableRow含2个子控件,第二个TableRow含3个,第三个TableRow含4个,那么该TableLayout的列数为4.

三、TableLayout可设置的属性详解

TableLayout可设置的属性包括全局属性及单元格属性。

1、全局属性也即列属性,有以下3个参数:

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

android:shrinkColumns 设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。

android:collapseColumns 设置要隐藏的列。

示例:

android:stretchColumns="0" 第0列可伸展

android:shrinkColumns="1,2" 第1,2列皆可收缩

android:collapseColumns="*" 隐藏所有行

说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的layout_height)

2、单元格属性,有以下2个参数:

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="表1:全局设置:列属性设置"
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="表2:单元格设置:指定单元格属性设置"
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="第0列"/>
<Button android:text="第1列"/>
<Button android:text="第2列"/>
</TableRow>
<TableRow>
<TextView android:text="我被指定在第2列" android:layout_column="2"/>
</TableRow>
<TableRow>
<TextView
android:text="我跨1到2列,不信你看!"
android:layout_column="1"
android:layout_span="2"
/>
</TableRow>
</TableLayout>
<!-- 第3个TableLayout,使用可伸展特性布局-->
<TextView
android:text="表3:应用一,非均匀布局"
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>
<TextView
android:text="表5:应用三,均匀布局"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:padding="6dip"
>
<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>
<Button android:text="四个个字" android:layout_width="1dip"></Button>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
</TableRow>
</TableLayout>
</LinearLayout>

以上内容是小逼给大家介绍的Android TableLayout表格布局,希望对大家有所帮助!

相关文章

  • Android实现上传文件到服务器实例详解

    Android实现上传文件到服务器实例详解

    本篇文章详细介绍了Android实现上传文件到服务器实例详解,实现了文件每隔5秒进行上传,有需要的可以了解一下。
    2016-11-11
  • Android开发App启动流程与消息机制详解

    Android开发App启动流程与消息机制详解

    这篇文章主要为大家介绍了Android开发App启动流程与消息机制详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Android内部存储改变读取权限的方法

    Android内部存储改变读取权限的方法

    今天小编就为大家分享一篇Android内部存储改变读取权限的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Android  Spinner列表选择框的应用

    Android Spinner列表选择框的应用

    这篇文章主要介绍了Android Spinner列表选择框的应用的相关资料,这里对spinner的属性和常用事件和数据绑定都做了介绍,需要朋友可以参考下
    2017-07-07
  • Android banner的使用详解与示例

    Android banner的使用详解与示例

    小编给大家分享一下android如何实现banner轮播图无限轮播效果,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧
    2021-11-11
  • Android WebView使用的技巧与一些坑

    Android WebView使用的技巧与一些坑

    这篇文章主要为大家详细介绍了Android WebView使用的技巧与一些坑,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android自定义View实现简单水波纹效果

    Android自定义View实现简单水波纹效果

    这篇文章主要为大家详细介绍了Android自定义View实现简单水波纹效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • Android自定View实现滑动验证效果的代码

    Android自定View实现滑动验证效果的代码

    这篇文章主要介绍了Android自定View实现滑动验证效果,代码分为自定义属性代码和自定义view代码及使用方法,本文给大家介绍的非常详细,需要的朋友可以参考下
    2021-12-12
  • 浅谈关于Android路由的实现

    浅谈关于Android路由的实现

    本篇文章主要介绍了浅谈关于Android路由的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Android自定义View实现数独游戏

    Android自定义View实现数独游戏

    这篇文章主要为大家详细介绍了Android自定义View实现数独游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12

最新评论