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 ...
随机推荐
- iOS 第三方框架-MJRefresh
MJRefresh是一款非常好用的上拉下拉第三方库,使用也很简单.github地址: https://github.com/CoderMJLee/MJRefresh . 下拉刷新 官方给过来的例子很简 ...
- FastReport问题整理(转)
FastReport问题整理 博客分类: 软件开发 部分来自网上,部分来自网友,部分来自Demo如果有新的内容,会不断更新.. 更新历史: 2009-02-27 加入套打方案全攻略(原:jinzh ...
- Oracle 性能调优
在 oracle 中效率排行, 表连接>exist>not exist>in>no in 并且使用in 查询 会有查询条件数量不能超过1000 的限制: 简单提高效率可以使用 ...
- Linux服务器---安装mysql
安装mysql 1.检测是否已安装mysql [root@localhost bin]# rpm -qa | grep mysql mysql-libs-5.1.71-1.el6.i686 [root ...
- HexDump.java解析,android 16进制转换
HexDump.java解析android 16进制转换 package com.android.internal.util; public class HexDump { private final ...
- ubuntu14.04无法安装Curl,需要先升级sudo apt-get update
ubuntu14.04无法安装Curl,需要先升级sudo apt-get updatesudo apt-get updatesudo apt-get install curl------------ ...
- ThinkPHP内置日志记录
ThinkPHP内置日志记录日志记录http://document.thinkphp.cn/manual_3_2.html#log 日志的处理工作是由系统自动进行的,在开启日志记录的情况下,会记录下允 ...
- jquery-easyui combobox combogrid 级联不可编辑实例
jquery-easyui combobox combogrid 级联不可编辑实例 如何让jquery-easyui的combobox像select那样不可编辑?为combobox添加editable ...
- ELK学习笔记之Logstash详解
0x00 Logstash概述 官方介绍:Logstash is an open source data collection engine with real-time pipelining cap ...
- Python 线程调用
简介: Python 线程可以通过主线程,调用线程来执行其他命令, 为Python提供更方便的使用. 并发线程测试 # 命令调用方式 import threading,time # 定义每个线程要运行 ...