原文:Android零基础入门第29节:善用TableLayout表格布局,事半功倍

前面学习了线性布局和相对布局,线性布局虽然方便,但如果遇到控件需要排列整齐的情况就很难达到要求,用相对布局又比较麻烦,为此Android系统中提供了表格布局。

一、认识TableLayout

表格布局就是让控件以表格的形式来排列控件,只要将控件放在单元格中,控件就可以整齐地排列,使用TableLayout标签。

TableLayout继承了 LinearLayout,因此它的本质依然是线性布局管理器。每次向TableLayout中添加一个TableRow,该TableRow就是一个表格行,TableRow也是容器,因此它也可以不断地添加其他组件,每添加一个子组件该表格就增加一列。如果直接向TableLayout中添加组件,那么这个组件将直接占用一行。

在表格布局中,列的宽度由该列中最宽的那个单元格决定,整个表格布局的宽度则取决于父容器的宽度(默认总是占满父容器本身)。

在表格布局管理器中,可以为单元格设置如下3种行为方式。

  • Shrinkable:如果某个列被设为Shrinkable,那么该列的所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度。

  • Stretchable:如果某个列被设为Stretchable,那么该列的所有单元格的宽度可以被拉伸,以保证组件能完全填满表格空余空间。

  • Collapsed:如果某个列被设为Collapsed,那么该列的所有单元格会被隐藏。

TableLayout继承了 LinearLayout,因此它完全可以支持LinearLayout所支持的全部XML属性。除此之外,TableLayout还支持如下表所示的XML属性和相关方法。

二、示例

接下来通过一个简单的示例程序来学习TableLayout的使用用法。

同样使用WidgetSample工程,继续使用app/main/res/layout/目录下的activity_main.xml文件,在其中填充如下代码片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!-- 定义第一个表格布局,指定第2列允许收缩,第3列允许拉伸 -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:stretchColumns="2">
<!-- 直接添加按钮,它自己会占一行 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="独自一行的按钮"/>
<!-- 添加一个表格行 -->
<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> <!-- 定义第2个表格布局 ,指定第2列隐藏-->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:collapseColumns="1">
<!-- 添加一个表格行 -->
<TableRow>
<!-- 为该表格行添加三个按钮 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按钮1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按钮2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按钮3"/>
</TableRow>
</TableLayout> <!-- 定义第3个表格布局,指定第2列和第3列可以被拉伸-->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1,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="拉伸的按钮" />
</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="拉伸的按钮" />
</TableRow>
</TableLayout>
</LinearLayout>

上面页面中定义了 3个TableLayout,3个TableLayout中粗体字代码指定了它们对各列的控制行为。

  • 第1个TableLayout,指定第2列允许收缩,第3列允许拉伸。

  • 第2个TableLayout,指定第2列被隐藏。

  • 第3个TableLayout,指定第2列和第3列允许拉伸。

运行程序,可以看到下图所示界面效果。

需要注意的是TableRow不需要设置宽度layout_width和高度layoutJieight,其宽度一定是match_parent,即自动填充父容器,高度一定为wrap_content,即根据内容改变高度。但对于TableRow中的其他控件来说,是可以设置宽度和高度的,但必其须是 wrap_content 或者 fill_parent。

到此,TableLayout的示例结束,关于TableLayout的更多用法可以多动手练习。


今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入Android零基础入门技术讨论微信群,共同成长!

此文章版权为微信公众号分享达人秀(ShareExpert)——鑫鱻所有,若转载请备注出处,特此声明!

往期总结分享:

Android零基础入门第1节:Android的前世今生

Android零基础入门第2节:Android 系统架构和应用组件那些事

Android零基础入门第3节:带你一起来聊一聊Android开发环境

Android零基础入门第4节:正确安装和配置JDK, 高富帅养成第一招

Android零基础入门第5节:善用ADT Bundle, 轻松邂逅女神

Android零基础入门第6节:配置优化SDK Manager, 正式约会女神

Android零基础入门第7节:搞定Android模拟器,开启甜蜜之旅

Android零基础入门第8节:HelloWorld,我的第一趟旅程出发点

Android零基础入门第9节:Android应用实战,不懂代码也可以开发

Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio

Android零基础入门第11节:简单几步带你飞,运行Android Studio工程

Android零基础入门第12节:熟悉Android Studio界面,开始装逼卖萌

Android零基础入门第13节:Android Studio配置优化,打造开发利器

Android零基础入门第14节:使用高速Genymotion,跨入火箭时代

Android零基础入门第15节:掌握Android Studio项目结构,扬帆起航

Android零基础入门第16节:Android用户界面开发概述

Android零基础入门第17节:TextView属性和方法大全

Android零基础入门第18节:EditText的属性和使用方法

Android零基础入门第19节:Button使用详解

Android零基础入门第20节:CheckBox和RadioButton使用大全

Android零基础入门第21节:ToggleButton和Switch使用大全

Android零基础入门第22节:ImageView的属性和方法大全

Android零基础入门第23节:ImageButton和ZoomButton使用大全

Android零基础入门第24节:自定义View简单使用,打造属于你的控件

Android零基础入门第25节:简单且最常用的LinearLayout线性布局

Android零基础入门第26节:两种对齐方式,layout_gravity和gravity大不同

Android零基础入门第27节:正确使用padding和margin

Android零基础入门第28节:轻松掌握RelativeLayout相对布局

Android零基础入门第29节:善用TableLayout表格布局,事半功倍的更多相关文章

  1. Android零基础入门第30节:两分钟掌握FrameLayout帧布局

    原文:Android零基础入门第30节:两分钟掌握FrameLayout帧布局 前面学习了线性布局.相对布局.表格布局,那么本期来学习第四种布局--FrameLayout帧布局. 一.认识FrameL ...

  2. Android零基础入门第32节:新推出的GridLayout网格布局

    原文:Android零基础入门第32节:新推出的GridLayout网格布局 本期主要学习的是网格布局是Android 4.0新增的布局,和前面所学的TableLayout表格布局 有点类似,不过他有 ...

  3. Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局

    原文:Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局 前面几期基本学习了Android开发中常用的四种布局,之所以把AbsoluteLayout放在后面来学习 ...

  4. Android零基础入门第58节:数值选择器NumberPicker

    原文:Android零基础入门第58节:数值选择器NumberPicker 上一期学习了日期选择器DatePicker和时间选择器TimePicker,是不是感觉非常简单,本期继续来学习数值选择器Nu ...

  5. Android零基础入门第59节:AnalogClock、DigitalClock和TextClock时钟组件

    原文:Android零基础入门第59节:AnalogClock.DigitalClock和TextClock时钟组件 在前面一期,我们学习了DatePicker和TimePicker,在实际开发中其不 ...

  6. Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker

    原文:Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker 在实际开发中,经常会遇见一些时间选择器.日期选择器.数字选择器等需求,那么从本期开始来学习And ...

  7. Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图

    原文:Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图 前面两期学习了 ViewAnimator及其子类ViewSwitcher的使用,以及ViewSwitcher的 ...

  8. Android零基础入门第55节:ImageSwitcher和TextSwitcher使用

    原文:Android零基础入门第55节:ImageSwitcher和TextSwitcher使用 上一期我们了解了ViewAnimator组件和ViewSwitcher组件的使用,你都掌握了吗?本期一 ...

  9. Android零基础入门第54节:视图切换组件ViewSwitcher

    原文:Android零基础入门第54节:视图切换组件ViewSwitcher 前面三期学习了ProgressBar系列组件,那本期开始一起来学习ViewAnimator组件. 一.ViewAnimat ...

随机推荐

  1. js获取计算后的样式表

    在编写html时,使用dom对象的style属性可以获取标签里的style属性,但是不能获取单独css样式文件或者style标签的属性值 <div style="width:10px& ...

  2. Android - HelloWorld的Layout内容

    Android - HelloWorld的Layout内容 本文地址: http://blog.csdn.net/caroline_wendy 作为最基础的Android程序, HelloWorld的 ...

  3. Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/vm10-0-0-19

    输入:service mysqld start 报错: Starting MySQL.. ERROR! The server quit without updating PID file (/usr/ ...

  4. windows - Cygwin和MinGW有什么区别?(MinGW从Cygwin 1.3.3版本中分离出来)

    windows - Cygwin和MinGW有什么区别? 我想让我的C ++项目跨平台,我正在考虑使用Cygwin / MinGW. 但是他们之间有什么区别呢? 另一个问题是,如果没有Cygwin / ...

  5. 利用WPF建立自己的3d gis软件(非axhost方式)(十三)万能的用户层接口,(强大的WPF)

    原文:利用WPF建立自己的3d gis软件(非axhost方式)(十三)万能的用户层接口,(强大的WPF) 先下载SDK:https://pan.baidu.com/s/1M9kBS6ouUwLfrt ...

  6. 漫步Unity3D(一)

    前言 采用Unity它已经将近半年的时间,虽然项目仅仅是一个半成品.但Unity熟几乎相同的游戏.在这里,在使用过程中遇到的问题,再梳.不涉及过于详细的功能和代码.但是,假设他们将参与开发一个在线知识 ...

  7. WPF 修改图片颜色

    原文:WPF 修改图片颜色 本文告诉大家如何修改图片的颜色,如去掉图片的蓝色 在 WPF 可以使用很多图片处理的方法,本文告诉大家的是一个图片处理,可以把处理的图片保存在文件. 在阅读本文,我假设大家 ...

  8. apply plugin: 'idea' --- gradle idea

    如果你的项目使用了Gradle作为构建工具,那么你一定要使用Gradle来自动生成IDE的项目文件,无需再手动的将源代码导入到你的IDE中去了. 如果你使用的是eclipse,可以在build.gra ...

  9. Java SE学问Random

    这篇文章是在网络上的文章内容摘要学习以及自己的小练习,感谢您的无私分享. 昨天在项目中想使用几个随机数.结果使用Random时竟然出现随机数同样的情况.忍不住查了些资料. 现将代码总结例如以下: pa ...

  10. nditer —— numpy.ndarray 多维数组的迭代

    1. Single array iteration >>> a = np.arange(6).reshape(2,3) >>> for x in np.nditer ...