Android TableLayout中的使用说明
TableLayout特点:
1)TableLayout和我们平时在网页上见到的Table有所不同,TableLayout没有边框的
2)它是由多个TableRow对象组成,每个TableRow可以有0个或多个单元格,每个单元格就是一个View。这些TableRow,单元格不能
设置layout_width,宽度默认是fill_parent的,只有高度layout_height可以自定义,默认是
wrap_content。
3)单元格可以为empty,并且通过android:layout_column可以设置index值实现跳开某些单元格。在TableRow之间
4)添加View,设置layout_height以及背景色,就可以实现一条间隔线。android:layout_span可以设置合并几个单元格:
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TableRow>
- <TextView
- android:text="column1"
- android:padding="3dip" />
- <TextView
- android:text="column2"
- android:padding="3dip" />
- <TextView
- android:text="column3"
- android:padding="3dip" />
- </TableRow>
- <TableRow>
- <TextView
- android:text="column11"
- android:visibility="invisible"/> //cell不见了
- <TextView
- android:text="左边的invisible"
- android:gravity="right"
- android:padding="3dip" />
- <Button
- android:id="@+id/go"
- android:text="go"
- android:padding="3dip" />
- <Button
- android:text="cancel"
- android:padding="3dip" />
- </TableRow>
- <View //间隔线
- android:layout_height="2dip"
- android:background="#F00" />
- <TableRow>
- <TextView
- android:text="右边的cell empty" />
- <TextView
- android:layout_column="2"
- android:text="跳开empty cell"
- android:padding="3dip" />
- </TableRow>
- <TableRow>
- <TextView
- android:text="合并3个单元格"
- android:layout_span="3"
- android:gravity="center_horizontal"
- android:background="#FFC0C0C0"
- android:textColor="#f00"
- android:padding="3dip" />
- </TableRow>
- </TableLayout>
没有设置收缩/伸展效果

注意,原来没有添加 android:padding="3dip" 的,发现那些column会凑在一起的,没有空白间隔!明显看到,那个cancel按钮被挤到几乎看不见了!这时候需要使用
1)android:shrinkColumns="可收缩的column",
2)android:stretchColumns="可伸展的column"。
android:shrinkColumns和android:stretchColumns的值都是以0开始的index,但必须是string值,即
用"1,2,5"来表示。可以用"*"来表示all
columns。而且同一column可以同时设置为shrinkable和stretchable。
如果使用TableLayout类的setColumnShrinkable/setColumnStretchable (int
columnIndex, boolean
isShrinkable)就麻烦些了,需要一个一个column来设置。也可以使用TableLayout的
setShrinkAllColumns/setStretchAllColumns来设置all columns。
判断这些column是否shrinkable或stretchable,可以调用
isColumnShrinkable/isColumnStretchable(int
columnIndex),isShrinkAllColumns()/isStretchAllColumns()。
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:shrinkColumns="0" > // 设置第一个column可收缩
- <TableRow>
- <TextView
- android:text="column1"
- android:padding="3dip" />
- <TextView
- android:text="column2"
- android:padding="3dip" />
- <TextView
- android:text="column3"
- android:padding="3dip" />
- </TableRow>
- <TableRow>
- <TextView
- android:text="column11"
- android:visibility="invisible"/>
- <TextView
- android:text="左边的invisible"
- android:gravity="right"
- android:padding="3dip" />
- <Button
- android:id="@+id/go2"
- android:text="go2"
- android:padding="3dip" />
- <Button
- android:text="cancel"
- android:padding="3dip" />
- </TableRow>
- <View
- android:layout_height="2dip"
- android:background="#F00" />
- <TableRow>
- <TextView
- android:text="右边的cell empty" />
- <TextView
- android:layout_column="2"
- android:text="跳开empty cell"
- android:padding="3dip" />
- <TextView
- android:text="123456789"
- android:padding="3dip" />
- </TableRow>
- </TableLayout>
可收缩column效果

现在可以看到第一个column为了让第4个column完整显示,而收缩得内容分为几行显示!
而可伸展column的效果就是在其他column可以完整显示时,该column就会伸展,占最多空间:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"> // 设置第二个column可伸展
<TableRow>
<TextView
android:text="column1"
android:padding="3dip" />
<TextView
android:text="column2"
android:gravity="right"
android:padding="3dip" />
<TextView
android:text="column3"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="column1"
android:padding="3dip" />
<TextView
android:text="column2"
android:gravity="right"
android:padding="3dip" />
<TextView
android:text="column3"
android:padding="3dip" />
</TableRow>
</TableLayout>
可伸展column效果

而动态隐藏column,可以调用TableLayout.setColumnCollapsed (int
columnIndex, boolean isCollapsed)来指定相应的column。另外TableLayout类的boolean
isColumnCollapsed (int columnIndex)能够判断指定的column是否隐藏。
TableLayout可以用来做网页上的Form显示效果,看看官方的sample:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="@string/table_layout_10_user"
android:textStyle="bold"
android:gravity="right"
android:padding="3dip" />
<EditText android:id="@+id/username"
android:text="@string/table_layout_10_username_text"
android:padding="3dip"
android:scrollHorizontally="true" />
</TableRow>
<TableRow>
<TextView
android:text="@string/table_layout_10_password"
android:textStyle="bold"
android:gravity="right"
android:padding="3dip" />
<EditText android:id="@+id/password"
android:text="@string/table_layout_10_password_text"
android:password="true"
android:padding="3dip"
android:scrollHorizontally="true" />
</TableRow>
<TableRow
android:gravity="right">
<Button android:id="@+id/cancel"
android:text="@string/table_layout_10_cancel" />
<Button android:id="@+id/login"
android:text="@string/table_layout_10_login" />
</TableRow>
</TableLayout>
Form效果

Android TableLayout中的使用说明的更多相关文章
- Android Studio中的Module,Facet
详细内容请参看 http://www.jetbrains.com/idea/webhelp/facet.html 以及 http://www.jetbrains.com/idea/webhelp/an ...
- Android Studio中配置及使用OpenCV示例
Android Studio配置及使用OpenCV 前言:最近在做项目移植,项目较大,在Eclipse中配置的Jni及OpenCV环境没任何问题,但是迁移到Studio中就问题一大堆,网上也找了一些资 ...
- 在 Android开发中,性能优化策略十分重要
在 Android开发中,性能优化策略十分重要本文主要讲解性能优化中的布局优化,希望你们会喜欢.目录 示意图 1. 影响的性能 布局性能的好坏 主要影响 :Android应用中的页面显示速度 2. 如 ...
- Android studio中的6大布局
1.相对布局代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns: ...
- 从Android设备中提取内核和逆向分析
本文博客链接:http://blog.csdn.net/qq1084283172/article/details/57074695 一.手机设备环境 Model number: Nexus 5 OS ...
- Android学习探索之Java 8 在Android 开发中的应用
前言: Java 8推出已经将近2年多了,引入很多革命性变化,加入了函数式编程的特征,使基于行为的编程成为可能,同时减化了各种设计模式的实现方式,是Java有史以来最重要的更新.但是Android上, ...
- Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder
Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...
- Google官方关于Android架构中MVP模式的示例续-DataBinding
基于前面的TODO示例,使用Data Binding库来显示数据并绑定UI元素的响应动作. 这个示例并未严格遵循 Model-View-ViewModel 或 Model-View-Presenter ...
- android studio 中移除module和恢复module
一.移除Android Studio中module 在Android Studio中想要删除某个module时,在Android Studio中选中module,右键发现没有delete,如图: An ...
随机推荐
- Here we take a closer look at the Jordans Unveil
Here we take a closer look at the Jordans Unveil. This Mens release is both unique and striking. The ...
- 9/252D图的画法
我们在介绍之前先想想2D图的一些元素 我在这里按我的思路写下一些: 坐标轴(尺度,区间..),线条(样式,颜色...),图和线的标签和注释,图像大小,图像里图片的排版(一张图像中多张图) 下面我们将分 ...
- SQL Server查询中特殊字符的处理方法
SQL Server查询中,经常会遇到一些特殊字符,比如单引号“'”等,这些字符的处理方法,是SQL Server用户都应该需要知道的. 我们都知道SQL Server查询过程中,单引号“'”是特殊字 ...
- pandas练习(一)------ 了解数据
探索Chipotle快餐数据 (相关数据见github) 步骤1 导入pandas库 import pandas as pd 步骤2 导入数据集 path1 = "./data/chipot ...
- Java设计模式应用——适配器模式
性能监控系统中,存在告警模块和报表模块,告警结果和报表结果都需要导出. 由于告警开发进度较快,已经实现了excel导出.csv导出.zip导出功能,现在报表需要excel导出.csv导出.pdf导出功 ...
- Java(20~24)
1.Collection中的集合称为单列集合,Map中的集合称为双列集合(键值对集合). 2.Map常用方法:map.put() map.get() map.remove() map.ke ...
- firefox历史版本下载链接
http://ftp.mozilla.org/pub/firefox/releases firefox版本42以上的用不了firebug,需要装版本42以下的,否则用不了
- linux rsync同步工具
linux rsync同步工具 1.rsync介绍rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具.rsync软件适用于unix/linux/windows ...
- SNMP学习笔记之SNMP TRAP简介、流程以及使用Python实现接受Trap信息
0x00 SNMP TRAP简介 SNMP(Simple Network Management Protocol) trap是一种很有用,但是也容易让人难以理解的协议. 虽然名字叫做简单网络管理协议, ...
- Observer模式实践
Observer 模式在实践中的应用场景: 为 Point 类设计一个数据绑定机制,当其坐标 x 或 y 被更改时,可以通知外界其更改的过程.将更改过程打印在控制台上.考虑使用松耦合设计. 代码: # ...