7.5 TableLayout布局详解

TableLayout表格布局,将子元素的位置分配到行或列中。TableLayout布局由许多的TableRow(行) 组成,它没有列的概念,列是由行中的控件数目决定的。TableLayout布局也是实际中常用的布局方式。
TableLayout布局不会显示行、列 、单元格的边框线。TableLayout布局应用如图7-17所示。
 

图7-17 TableLayout1
TableLayout布局文件请参考代码清单7-20,完整代码请参考chapter7_5工程中tablelayout1.xml代码部分(chapter7_5/res/layout/tablelayout1.xml)。
【代码清单7-20】
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TableRow>
<TextView android:text="@string/row1_1" android:padding="3dip" />
<TextView android:text="@string/row1_2" android:padding="3dip" />
<TextView android:text="@string/row1_3" android:padding="3dip" />
</TableRow>
<TableRow>
<TextView android:text="@string/row2_1" android:padding="3dip" />
<TextView android:text="@string/row2_2" android:padding="3dip" />
<TextView android:text="@string/row2_3" android:padding="3dip" />
</TableRow>
<TableRow>
<TextView android:text="@string/row3_1" android:padding="3dip" />
<TextView android:text="@string/row3_2" android:padding="3dip" />
<TextView android:text="@string/row3_3" android:padding="3dip" />
</TableRow>
<TableRow>
<TextView android:text="@string/row4_1" android:padding="3dip" />
<TextView android:text="@string/row4_2" android:padding="3dip" />
<TextView android:text="@string/row4_3" android:padding="3dip" />
</TableRow>
<TableRow>
<TextView android:text="@string/row5_1" android:padding="3dip" />
<TextView android:text="@string/row5_2" android:padding="3dip" />
<TextView android:text="@string/row5_3" android:padding="3dip" />
</TableRow>
<TableRow>
<TextView android:text="@string/row6_1" android:padding="3dip" />
<TextView android:text="@string/row6_2" android:padding="3dip" />
<TextView android:text="@string/row6_3" android:padding="3dip" />
</TableRow>

</TableLayout>
下面再看一个合并单元格TableLayou布局应用图7-18所示。
 

图7-18 TableLayout2
TableLayout布局文件请参考代码清单7-21,完整代码请参考chapter7_5工程中tablelayout2.xml代码部分(chapter7_5/res/layout/tablelayout2.xml)。
【代码清单7-21】
<?xml version="1.0" encoding="utf-8"?>
<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">
<TableRow>
<TextView android:text="@string/row1_1" android:padding="3dip" />
<TextView android:text="@string/row1_2" android:padding="3dip" />
<TextView android:text="row1_3" android:padding="3dip" />
</TableRow>

<TableRow>
<TextView android:text="@string/row2_1" android:padding="3dip" />
<TextView android:text="@string/row2_2" android:padding="3dip"
android:layout_span="2" android:background="#ff0060" />
</TableRow>

<TableRow>
<TextView android:text="@string/row3_1" android:padding="3dip" />
<!-- android:layout_column="1" -->
<TextView android:text="@string/row3_2" android:padding="3dip" />
<TextView android:text="row3_3" android:padding="3dip" />
</TableRow>

<TableRow>
<TextView android:text="@string/row4_1" android:padding="3dip" />
<TextView android:text="@string/row4_2" android:padding="3dip" />
<TextView android:text="row4_3" android:padding="3dip" />
</TableRow>

<TableRow>
<TextView android:text="@string/row5_1" android:padding="3dip" />
<TextView android:text="@string/row5_2" android:padding="3dip" />
<TextView android:text="row5_3" android:padding="3dip" />
</TableRow>

<TableRow>
<TextView android:text="@string/row6_1" android:padding="3dip" />
<TextView android:text="@string/row6_2" android:padding="3dip" />
<TextView android:text="row6_3" android:padding="3dip" />
</TableRow>
</TableLayout>
TableLayout标签内的android:stretchColumns="0,1,2"属性是第一列、第二列、第三列自动调节列宽。&lt;TableRow>内的TextView控件中的属性android:layout_span="2"是合并一个单元格,如图7-18显示的粉色背景就是合并的单元格。
                                        出自《Android开发案例驱动教程》第七章

时间: 2024-09-10 02:44:28

7.5 TableLayout布局详解的相关文章

Android 表格布局TableLayout示例详解_Android

一.表格布局 TableLayout 表格布局TableLayout以行列的形式管理子元素,每一行是一个TableRow布局对象,当然也可以是普通的View对象,TableRow离每放一个元素就是一列,总列数由列数最多的那一行决定. 我们看一个例子: <?xml version="1.0″ encoding="utf-8″?> <TableLayout android:id="@+id/TableLayout01″ android:layout_width=

Android开发-之五大布局详解_Android

在html中大家都知道布局是什么意思了,简单来说就是将页面划分模块,比如html中的div.table等.那么Android中也是这样的.Android五大布局让界面更加美化,开发起来也更加方便.当然布局方式不一样应用的地方也不一样,当然了有的布局方式也是可以相互转换和嵌套使用的.它们都各有各的优缺点,具体页面要怎么布局还是得看开发需求,但是用的最多的还是相对布局.线性布局以及相对布局和线性布局的嵌套使用.当然,我说的是安卓,并没有指定是安卓手机,比如平板.智能家居(电视...)很多都是Andr

Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)

  [Android布局学习系列]   1.Android 布局学习之--Layout(布局)详解一   2.Android 布局学习之--Layout(布局)详解二(常见布局和布局参数)   3.Android 布局学习之--LinearLayout的layout_weight属性   4.Android 布局学习之--LinearLayout属性baselineAligned的作用及baseline      Layout Parameters(布局参数):            在XML文

JS组件Bootstrap Table布局详解_javascript技巧

Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: 表格类下表样式可用于表格中: <tr>, <th> 和 <td> 类下表的类可用于表格的行或者单元格: 基本的表格 如果您想要一个只带有内边距(padding)和水平分割的基本表,请添加 class .table,如下面实例所示: <div class="row"> <table class="table"&

Android LayoutInflater加载布局详解及实例代码

Android  LayoutInflater加载布局详解 对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? 2.加载layout文件之后,又是怎么变成供我们使用的View的? 3.我们定义View的时候,如果需要在布局中使用,则必须实现带AttributeSet参数的构造方法,这又是为什么呢? 既然在这篇文章提出来,那说明这三

TableLayout表格布局详解

一.Tablelayout简介       Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件.当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列. 当为View时,该View将独占一行. 二.TableLayout行列数的确定(最大的原则)      TableLayout的行数由开发人员直接指定,即有多少个TableRow对象(或View控件),就有多少行.TableLayout的列数等于含有最

Android 表格布局TableLayout示例详解

一.表格布局 TableLayout 表格布局TableLayout以行列的形式管理子元素,每一行是一个TableRow布局对象,当然也可以是普通的View对象,TableRow离每放一个元素就是一列,总列数由列数最多的那一行决定. 我们看一个例子: <?xml version="1.0″ encoding="utf-8″?> <TableLayout android:id="@+id/TableLayout01″ android:layout_width=

Android UI组件LinearLayout线性布局详解_Android

LinearLayout 线性布局,该布局的继承关系:   1. 什么是线性布局 通俗的说感觉起来和线有关,参照线的特点,有么是横向的,要么是竖向的. LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列(通过android:orientation属性来控制),按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失 2. 线性布局常用基本属性 - android:id - android:orientation - and

Android系统五大布局详解Layout

  我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit等等.那么我们平时看到的Android手机中那些漂亮的界面是怎么显示出来的呢?这就要用到Android的布局管理器了,网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了.      在分析布局之前,我们首先看看控件:Android中任何可视化的控件