`

Android UI学习 - TableLayout

 
阅读更多

该内容转载自:http://android.blog.51cto.com/268543/314262

 

   TableLayout和我们平时在网页上见到的Table有所不同,TableLayout没有边框的,它是由多个TableRow对象组成,每个TableRow可以有0个或多个单元格,每个单元格就是一个View。这些TableRow,单元格不能设置layout_width,宽度默认是fill_parent的,只有高度layout_height可以自定义,默认是wrap_content。

     单元格可以为empty,并且通过android:layout_column可以设置index值实现跳开某些单元格。在TableRow之间,添加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.  
  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.  
  18.     <TableRow> 
  19.         <TextView 
  20.           android:text="column11" 
  21.           android:visibility="invisible"/> //cell不见了 
  22.         <TextView 
  23.             android:text="左边的invisible" 
  24.             android:gravity="right" 
  25.             android:padding="3dip" /> 
  26.         <Button 
  27.             android:id="@+id/go" 
  28.             android:text="go"  
  29.             android:padding="3dip" /> 
  30.         <Button 
  31.             android:text="cancel" 
  32.             android:padding="3dip" /> 
  33.     </TableRow> 
  34.  
  35.     <View                               //间隔线 
  36.         android:layout_height="2dip" 
  37.         android:background="#F00" /> 
  38.  
  39.     <TableRow> 
  40.         <TextView 
  41.            android:text="右边的cell empty" /> 
  42.         <TextView 
  43.             android:layout_column="2" 
  44.             android:text="跳开empty cell" 
  45.             android:padding="3dip" /> 
  46.     </TableRow> 
  47.      
  48.     <TableRow> 
  49.         <TextView 
  50.             android:text="合并3个单元格" 
  51.             android:layout_span="3" 
  52.             android:gravity="center_horizontal" 
  53.             android:background="#FFC0C0C0" 
  54.             android:textColor="#f00" 
  55.             android:padding="3dip" /> 
  56.     </TableRow> 
  57. </TableLayout> 
没有设置收缩/伸展效果

Table1

    注意,原来没有添加 android:padding="3dip" 的,发现那些column会凑在一起的,没有空白间隔!明显看到,那个cancel按钮被挤到几乎看不见了!这时候需要使用android:shrinkColumns="可收缩的column",android:stretchColumns="可伸展的column"。
    android:shrinkColumnsandroid: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.  
  7.     <TableRow> 
  8.         <TextView 
  9.             android:text="column1" 
  10.             android:padding="3dip"  /> 
  11.         <TextView 
  12.             android:text="column2" 
  13.             android:padding="3dip"  /> 
  14.         <TextView 
  15.             android:text="column3" 
  16.             android:padding="3dip"  /> 
  17.     </TableRow> 
  18.  
  19.     <TableRow> 
  20.         <TextView 
  21.           android:text="column11" 
  22.           android:visibility="invisible"/> 
  23.         <TextView 
  24.             android:text="左边的invisible" 
  25.             android:gravity="right" 
  26.             android:padding="3dip" /> 
  27.         <Button 
  28.             android:id="@+id/go2" 
  29.             android:text="go2"  
  30.             android:padding="3dip" /> 
  31.         <Button 
  32.             android:text="cancel" 
  33.             android:padding="3dip" /> 
  34.     </TableRow> 
  35.  
  36.     <View 
  37.         android:layout_height="2dip" 
  38.         android:background="#F00" /> 
  39.  
  40.     <TableRow> 
  41.         <TextView 
  42.           android:text="右边的cell empty" /> 
  43.         <TextView 
  44.             android:layout_column="2" 
  45.             android:text="跳开empty cell" 
  46.             android:padding="3dip" /> 
  47.         <TextView 
  48.             android:text="123456789" 
  49.             android:padding="3dip" /> 
  50.     </TableRow> 
  51. </TableLayout> 
可收缩column效果

Table2

     现在可以看到第一个column为了让第4个column完整显示,而收缩得内容分为几行显示!
 
     而可伸展column的效果就是在其他column可以完整显示时,该column就会伸展,占最多空间:
 
  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:stretchColumns="1"> // 设置第二个column可伸展
  6.  
  7.    <TableRow> 
  8.         <TextView 
  9.             android:text="column1" 
  10.             android:padding="3dip" /> 
  11.         <TextView 
  12.             android:text="column2" 
  13.             android:gravity="right" 
  14.             android:padding="3dip" /> 
  15.         <TextView 
  16.             android:text="column3" 
  17.             android:padding="3dip"  /> 
  18.     </TableRow> 
  19.  
  20.     <TableRow> 
  21.         <TextView 
  22.             android:text="column1" 
  23.             android:padding="3dip" /> 
  24.         <TextView 
  25.             android:text="column2" 
  26.             android:gravity="right" 
  27.             android:padding="3dip" /> 
  28.         <TextView 
  29.             android:text="column3" 
  30.             android:padding="3dip"  /> 
  31.     </TableRow> 
  32. </TableLayout> 
可伸展column效果

Table3

 
      而动态隐藏column,可以调用TableLayout.setColumnCollapsed (int columnIndex, boolean isCollapsed)来指定相应的column。另外TableLayout类的boolean isColumnCollapsed (int columnIndex)能够判断指定的column是否隐藏。
 
      TableLayout可以用来做网页上的Form显示效果,看看官方的sample:
 
  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:stretchColumns="1"> 
  6.  
  7.    <TableRow> 
  8.        <TextView 
  9.            android:text="@string/table_layout_10_user" 
  10.            android:textStyle="bold" 
  11.            android:gravity="right" 
  12.            android:padding="3dip" /> 
  13.  
  14.        <EditText android:id="@+id/username" 
  15.            android:text="@string/table_layout_10_username_text" 
  16.            android:padding="3dip" 
  17.            android:scrollHorizontally="true" /> 
  18.    </TableRow> 
  19.  
  20.    <TableRow> 
  21.        <TextView 
  22.            android:text="@string/table_layout_10_password" 
  23.            android:textStyle="bold" 
  24.            android:gravity="right" 
  25.            android:padding="3dip" /> 
  26.  
  27.        <EditText android:id="@+id/password" 
  28.            android:text="@string/table_layout_10_password_text" 
  29.            android:password="true" 
  30.            android:padding="3dip" 
  31.            android:scrollHorizontally="true" /> 
  32.    </TableRow> 
  33.  
  34.    <TableRow 
  35.        android:gravity="right"> 
  36.  
  37.        <Button android:id="@+id/cancel" 
  38.            android:text="@string/table_layout_10_cancel" /> 
  39.  
  40.        <Button android:id="@+id/login" 
  41.            android:text="@string/table_layout_10_login" /> 
  42.    </TableRow> 
  43. </TableLayout> 
Form效果

Table4

本文出自 “学习Android” 博客,请务必保留此出处http://android.blog.51cto.com/268543/314262

分享到:
评论

相关推荐

    Android---UI篇

    •Andorid---UI篇---TableLayout(表格布局) • •Android---UI篇---RelativeLayout(相对布局) • •Android---UI篇---GridView(网格布局) • •Android---UI篇---Gallery(画廊视图) • •Android---UI篇---...

    Android UI设计心得

    Android UI设计心得。包括线性布局(LinearLayout)、相对布局(RelativeLayout)、表格布局(TableLayout)、框架布局(FrameLayout)、绝对布局(AbsoluteLayout)。其中AbsoluteLayout在Android SDK2.3.3之后已经...

    Android UI LinearLayout权限级别与TableLayout混合使用,

    NULL 博文链接:https://yangguangfu.iteye.com/blog/678977

    Android.UI基础教程]

    第1部分AndroidUI基础 第1章 入门 1.1 HeUoWorld 1.2 Android应用程序的基本结构 1.2.1文件夹结构 1. 2.2.AndroidManifest 1.2.3 资源 1.3 AndroidUI基础 1.3.1 主屏幕和通知栏1.3.2 XM[布局 1.3.3 ACTIVI'[Y类 ...

    Android 基于TabLayout实现的TAB页效果 仿今日头条.rar

     你可以学习下在Android开发中,tablelayout 与viewpager如何关联,如何创建每个tag标签对应的Fragment,本源码中是创建5个标题并加入布局中,实际应用中,你可以根据需要添加或减少对应的TAb布局数量。  编译时请...

    Android中UI布局Layout

    线性布局LinearLayout 框架布局FrameLayOut RelativeLayout相对布局 绝对布局AbsoluteLayout TableLayout表格布局

    android核心组件(1)常用布局,adapter,handler,UI

    线性布局:lineerlayout,相对布局:relativeLayout,表格布局:tablelayout,列表布局:listview(listAdapter),表格布局:gridview(listadapter),imageadapter,绝对布局:absoluteLayout,标签布局:tablayout:1&gt;android....

    编写微信界面(UI界面设计-移动平台开发技术-gddrxy

    1. Android有五大布局对象,它们分别是FrameLayout(框架布局:不知道是不是这么翻译的),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。 2. Android常用控件: 1...

    Android应用开发揭秘pdf高清版

    15.6 AndroidUI优化 15.7 其他优化 15.7.1 zipalign 15.7.2 图片优化 15.8 小结 第五部分 扩展篇 第16章 Android NDK开发 16.1 AndroidNDK简介 16.2 安装和配置NDK开发环境 16.2.1 系统和软件需求 16.2.2 NDK开发...

    Android结合SQLite设计一款购物车

    具体可以在专栏 【Android开发基础】三种常见的适配器(Adapter)、 【AndroidUI设计】icon矢量图标应用(低内存复应用) 中学习到。 按照折扣,只有第一份享受折扣。(当然还有第n份多少折,其实都很简单,就是for...

    Android开发资料合集-World版!

    3、ANDROID UI LAYOUT 35 3.1、概述 35 3.2、线性布局(LINEAR LAYOUT) 36 3.3、相对布局(RELATIVE LAYOUT) 39 3.4、TABLELAYOUT 40 3.5、ABSOLUTELAYOUT 47 4、ANDROID UI 控件 48 4.1、IMAGEBUTTON 48 4.1.1、...

    Android和SQLite开发一个购物车功能,包括增删改查多选提交等

    具体可以在专栏 【Android开发基础】三种常见的适配器(Adapter)、 【AndroidUI设计】icon矢量图标应用(低内存复应用) 中学习到。 按照折扣,只有第一份享受折扣。(当然还有第n份多少折,其实都很简单,就是for...

    安卓开发实战微课第91-07罐:TableLayout_职场大菠菜出品

    第4-11罐从“Android视图与容器组件”、“如何控制UI界面”、“Android布局管理器介绍”、“第六大布局特性和演练”四个方面,讲解了Android提供的丰富用户界面组件基础,以及如何使用代码和XML分别控制用户界面。...

    Android开发案例驱动教程 配套代码

    4.1 Android UI组件概述 43 4.1.1 View 43 4.1.2 ViewGroup 44 4.1.3 布局管理器 44 4.2 UI设计工具 44 4.2.1 DroidDraw工具 44 4.2.2 ADT插件UI设计工具 46 4.3 事件处理模型 47 4.3.1 接口实现事件处理...

    Android实验指导.doc

    运行: 选运行的设备,可以是模拟器,也可以是真机(如果已经连接好真实手机的话): 模拟器运行: 真实手机调试: 实验二:界面设计:控件与布局 【目的】 Android编程基础,UI设计。 【要求】 1. 了解Android编程...

    Android移动应用开发实验指导书.docx

    (2)Android的布局方式有LinearLayout(线性布局)、FrameLayout(单帧布局)、RelativeLayout(相对布局)、TableLayout(表格布局)。 (3)Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)...

    Android UI组件LinearLayout线性布局详解

    LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列(通过android:orientation属性来控制),按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失 ...

    《Android应用开发揭秘》附带光盘代码.

     15.6 AndroidUI优化  15.7 其他优化  15.7.1 zipalign  15.7.2 图片优化  15.8 小结  第五部分 扩展篇  第16章 Android NDK开发  16.1 AndroidNDK简介  16.2 安装和配置NDK开发环境  16.2.1 系统和软件...

Global site tag (gtag.js) - Google Analytics