zoukankan      html  css  js  c++  java
  • 【转】android:TableLayout表格布局详解

    这篇博文包括的内容:

    1、TableLayout简介

    2、TableLayout行列数的确定

    3、TableLayout可设置的属性详解

    4、一个包含4个TableLayout布局的实例及效果图

    一、Tablelayout简介

           Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。

             当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。

             当为View时,该View将独占一行。

    二、TableLayout行列数的确定

               TableLayout的行数由开发人员直接指定,即有多少个TableRow对象(或View控件),就有多少行。

            TableLayout的列数等于含有最多子控件的TableRow的列数。如第一TableRow含2个子控件,第二个TableRow含3个,第三个TableRow含4个,那么该TableLayout的列数为4.

    三、TableLayout可设置的属性详解

    TableLayout可设置的属性包括全局属性及单元格属性。

    1、全局属性也即列属性,有以下3个参数:

    android:stretchColumns    设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。

    android:shrinkColumns     设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。

    android:collapseColumns 设置要隐藏的列。


    示例:

    android:stretchColumns="0"           第0列可伸展

    android:shrinkColumns="1,2"         第1,2列皆可收缩

    android:collapseColumns="*"         隐藏所有行

    说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的layout_height)

    2、单元格属性,有以下2个参数:

    android:layout_column    指定该单元格在第几列显示

    android:layout_span        指定该单元格占据的列数(未指定时,为1)

    示例:

    android:layout_column="1"    该控件显示在第1列

    android:layout_span="2"        该控件占据2列

    说明:一个控件也可以同时具备这两个特性。


    四、一个包含4个TableLayout布局的实例及效果图

      1     <?xml version="1.0" encoding="utf-8"?>  
    2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:orientation="vertical"
    4 android:layout_width="fill_parent"
    5 android:layout_height="fill_parent"
    6 android:padding="3dip"
    7 >
    8
    9 <!-- 第1个TableLayout,用于描述表中的列属性。第0列可伸展,第1列可收缩 ,第2列被隐藏-->
    10 <TextView
    11 android:text="表1:全局设置:列属性设置"
    12 android:layout_height="wrap_content"
    13 android:layout_width="wrap_content"
    14 android:textSize="15sp"
    15 android:background="#7f00ffff"/>
    16 <TableLayout
    17 android:id="@+id/table1"
    18 android:layout_width="fill_parent"
    19 android:layout_height="wrap_content"
    20 android:stretchColumns="0"
    21 android:shrinkColumns="1"
    22 android:collapseColumns="2"
    23 android:padding="3dip">
    24 <TableRow>
    25 <Button android:text="该列可伸展"/>
    26 <Button android:text="该列可收缩"/>
    27 <Button android:text="我被隐藏了"/>
    28 </TableRow>
    29
    30 <TableRow>
    31 <TextView android:text="我向行方向伸展,我可以很长 "/>
    32 <TextView android:text="我向列方向收缩,我可以很深"/>
    33 </TableRow>
    34
    35 </TableLayout>
    36
    37 <!-- 第2个TableLayout,用于描述表中单元格的属性,包括:android:layout_column 及android:layout_span-->
    38 <TextView
    39 android:text="表2:单元格设置:指定单元格属性设置"
    40 android:layout_height="wrap_content"
    41 android:layout_width="wrap_content"
    42 android:textSize="15sp"
    43 android:background="#7f00ffff"/>
    44 <TableLayout
    45 android:id="@+id/table2"
    46 android:layout_width="fill_parent"
    47 android:layout_height="wrap_content"
    48 android:padding="3dip">
    49 <TableRow>
    50 <Button android:text="第0列"/>
    51 <Button android:text="第1列"/>
    52 <Button android:text="第2列"/>
    53 </TableRow>
    54
    55 <TableRow>
    56 <TextView android:text="我被指定在第1列" android:layout_column="1"/>
    57 </TableRow>
    58
    59 <TableRow>
    60 <TextView
    61 android:text="我跨1到2列,不信你看!"
    62 android:layout_column="1"
    63 android:layout_span="2"
    64 />
    65 </TableRow>
    66
    67 </TableLayout>
    68
    69 <!-- 第3个TableLayout,使用可伸展特性布局-->
    70 <TextView
    71 android:text="表3:应用一,非均匀布局"
    72 android:layout_height="wrap_content"
    73 android:layout_width="wrap_content"
    74 android:textSize="15sp"
    75 android:background="#7f00ffff"/>
    76 <TableLayout
    77 android:id="@+id/table3"
    78 android:layout_width="fill_parent"
    79 android:layout_height="wrap_content"
    80 android:stretchColumns="*"
    81 android:padding="3dip"
    82 >
    83 <TableRow>
    84 <Button android:text="一" ></Button>
    85 <Button android:text="两字"></Button>
    86 <Button android:text="三个字" ></Button>
    87 </TableRow>
    88 </TableLayout>
    89
    90 <!-- 第4个TableLayout,使用可伸展特性,并指定每个控件宽度一致,如1dip-->
    91 <TextView
    92 android:text="表4:应用二,均匀布局"
    93 android:layout_height="wrap_content"
    94 android:layout_width="wrap_content"
    95 android:textSize="15sp"
    96 android:background="#7f00ffff"/>
    97 <TableLayout
    98 android:id="@+id/table4"
    99 android:layout_width="fill_parent"
    100 android:layout_height="wrap_content"
    101 android:stretchColumns="*"
    102 android:padding="3dip"
    103 >
    104 <TableRow>
    105 <Button android:text="一" android:layout_width="1dip"></Button>
    106 <Button android:text="两字" android:layout_width="1dip"></Button>
    107 <Button android:text="三个字" android:layout_width="1dip"></Button>
    108 </TableRow>
    109 </TableLayout>
    110 </LinearLayout>

    说明:第4个TableLayout里的均匀布局的均匀效果是有限的。其有限性体现在,当该行有N列,则每列的控件内容不能多于1/N。

    运行效果图:(如图1)

     

    参考书目:

    [1] 《android基础教程》,[美]Ed Burnette 著,张波,高朝勤,杨月等译,北京:人民邮电出版社,2009.11

    [2] 《android开发入门教程》,[美]Mark L. Murphy著,李雪飞,吴明晖译,北京:人民邮电出版社,2010.12

    [3] 《android核心技术与实例详解》,吴亚峰,索依娜,北京:电子工业出版社,2010.10

    作者:JustOneRoad
    出处:http://blog.csdn.net/JustOneRoad/article/details/6835915

  • 相关阅读:
    Session的使用与Session的生命周期
    Long-Polling, Websockets, SSE(Server-Sent Event), WebRTC 之间的区别与使用
    十九、详述 IntelliJ IDEA 之 添加 jar 包
    十八、IntelliJ IDEA 常用快捷键 之 Windows 版
    十七、IntelliJ IDEA 中的 Maven 项目初体验及搭建 Spring MVC 框架
    十六、详述 IntelliJ IDEA 创建 Maven 项目及设置 java 源目录的方法
    十五、详述 IntelliJ IDEA 插件的安装及使用方法
    十四、详述 IntelliJ IDEA 提交代码前的 Code Analysis 机制
    十三、IntelliJ IDEA 中的版本控制介绍(下)
    十二、IntelliJ IDEA 中的版本控制介绍(中)
  • 原文地址:https://www.cnblogs.com/armyant/p/2425087.html
Copyright © 2011-2022 走看看