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可以设置合并几个单元格:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TableRow>
  6. <TextView
  7. android:text="column1"
  8. android:padding="3dip"  />
  9. <TextView
  10. android:text="column2"
  11. android:padding="3dip"  />
  12. <TextView
  13. android:text="column3"
  14. android:padding="3dip"  />
  15. </TableRow>
  16. <TableRow>
  17. <TextView
  18. android:text="column11"
  19. android:visibility="invisible"/> //cell不见了
  20. <TextView
  21. android:text="左边的invisible"
  22. android:gravity="right"
  23. android:padding="3dip" />
  24. <Button
  25. android:id="@+id/go"
  26. android:text="go"
  27. android:padding="3dip" />
  28. <Button
  29. android:text="cancel"
  30. android:padding="3dip" />
  31. </TableRow>
  32. <View                               //间隔线
  33. android:layout_height="2dip"
  34. android:background="#F00" />
  35. <TableRow>
  36. <TextView
  37. android:text="右边的cell empty" />
  38. <TextView
  39. android:layout_column="2"
  40. android:text="跳开empty cell"
  41. android:padding="3dip" />
  42. </TableRow>
  43. <TableRow>
  44. <TextView
  45. android:text="合并3个单元格"
  46. android:layout_span="3"
  47. android:gravity="center_horizontal"
  48. android:background="#FFC0C0C0"
  49. android:textColor="#f00"
  50. android:padding="3dip" />
  51. </TableRow>
  52. </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()。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:shrinkColumns="0" > // 设置第一个column可收缩
  6. <TableRow>
  7. <TextView
  8. android:text="column1"
  9. android:padding="3dip"  />
  10. <TextView
  11. android:text="column2"
  12. android:padding="3dip"  />
  13. <TextView
  14. android:text="column3"
  15. android:padding="3dip"  />
  16. </TableRow>
  17. <TableRow>
  18. <TextView
  19. android:text="column11"
  20. android:visibility="invisible"/>
  21. <TextView
  22. android:text="左边的invisible"
  23. android:gravity="right"
  24. android:padding="3dip" />
  25. <Button
  26. android:id="@+id/go2"
  27. android:text="go2"
  28. android:padding="3dip" />
  29. <Button
  30. android:text="cancel"
  31. android:padding="3dip" />
  32. </TableRow>
  33. <View
  34. android:layout_height="2dip"
  35. android:background="#F00" />
  36. <TableRow>
  37. <TextView
  38. android:text="右边的cell empty" />
  39. <TextView
  40. android:layout_column="2"
  41. android:text="跳开empty cell"
  42. android:padding="3dip" />
  43. <TextView
  44. android:text="123456789"
  45. android:padding="3dip" />
  46. </TableRow>
  47. </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中的使用说明的更多相关文章

  1. Android Studio中的Module,Facet

    详细内容请参看 http://www.jetbrains.com/idea/webhelp/facet.html 以及 http://www.jetbrains.com/idea/webhelp/an ...

  2. Android Studio中配置及使用OpenCV示例

    Android Studio配置及使用OpenCV 前言:最近在做项目移植,项目较大,在Eclipse中配置的Jni及OpenCV环境没任何问题,但是迁移到Studio中就问题一大堆,网上也找了一些资 ...

  3. 在 Android开发中,性能优化策略十分重要

    在 Android开发中,性能优化策略十分重要本文主要讲解性能优化中的布局优化,希望你们会喜欢.目录 示意图 1. 影响的性能 布局性能的好坏 主要影响 :Android应用中的页面显示速度 2. 如 ...

  4. Android studio中的6大布局

    1.相对布局代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns: ...

  5. 从Android设备中提取内核和逆向分析

    本文博客链接:http://blog.csdn.net/qq1084283172/article/details/57074695 一.手机设备环境 Model number: Nexus 5 OS ...

  6. Android学习探索之Java 8 在Android 开发中的应用

    前言: Java 8推出已经将近2年多了,引入很多革命性变化,加入了函数式编程的特征,使基于行为的编程成为可能,同时减化了各种设计模式的实现方式,是Java有史以来最重要的更新.但是Android上, ...

  7. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

  8. Google官方关于Android架构中MVP模式的示例续-DataBinding

    基于前面的TODO示例,使用Data Binding库来显示数据并绑定UI元素的响应动作. 这个示例并未严格遵循 Model-View-ViewModel 或 Model-View-Presenter ...

  9. android studio 中移除module和恢复module

    一.移除Android Studio中module 在Android Studio中想要删除某个module时,在Android Studio中选中module,右键发现没有delete,如图: An ...

随机推荐

  1. iOS 第三方框架-MJRefresh

    MJRefresh是一款非常好用的上拉下拉第三方库,使用也很简单.github地址: https://github.com/CoderMJLee/MJRefresh . 下拉刷新 官方给过来的例子很简 ...

  2. FastReport问题整理(转)

    FastReport问题整理 博客分类: 软件开发   部分来自网上,部分来自网友,部分来自Demo如果有新的内容,会不断更新.. 更新历史: 2009-02-27 加入套打方案全攻略(原:jinzh ...

  3. Oracle 性能调优

    在 oracle 中效率排行, 表连接>exist>not exist>in>no in 并且使用in 查询 会有查询条件数量不能超过1000 的限制: 简单提高效率可以使用 ...

  4. Linux服务器---安装mysql

    安装mysql 1.检测是否已安装mysql [root@localhost bin]# rpm -qa | grep mysql mysql-libs-5.1.71-1.el6.i686 [root ...

  5. HexDump.java解析,android 16进制转换

    HexDump.java解析android 16进制转换 package com.android.internal.util; public class HexDump { private final ...

  6. ubuntu14.04无法安装Curl,需要先升级sudo apt-get update

    ubuntu14.04无法安装Curl,需要先升级sudo apt-get updatesudo apt-get updatesudo apt-get install curl------------ ...

  7. ThinkPHP内置日志记录

    ThinkPHP内置日志记录日志记录http://document.thinkphp.cn/manual_3_2.html#log 日志的处理工作是由系统自动进行的,在开启日志记录的情况下,会记录下允 ...

  8. jquery-easyui combobox combogrid 级联不可编辑实例

    jquery-easyui combobox combogrid 级联不可编辑实例 如何让jquery-easyui的combobox像select那样不可编辑?为combobox添加editable ...

  9. ELK学习笔记之Logstash详解

    0x00 Logstash概述 官方介绍:Logstash is an open source data collection engine with real-time pipelining cap ...

  10. Python 线程调用

    简介: Python 线程可以通过主线程,调用线程来执行其他命令, 为Python提供更方便的使用. 并发线程测试 # 命令调用方式 import threading,time # 定义每个线程要运行 ...