TableLayout的基本使用方式

1、TableLayout(表格布局)的样式,就像是一张表格。每个TableLayout,都由多个TableRow组成,每个TableRow就是一行,有几个TableRow就有几行。TableLayout不会显示行号和列号,也没有分割线,其行数和列数都可以进行操作。
下面是 3 (行) x 3(列) 的TableLayout,其xml布局文件table_relative01.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow >
        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1"      
        ></Button>
        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2"      
        ></Button>
        <Button
            android:id="@+id/button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"      
        ></Button>
        <Button
            android:id="@+id/button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"      
        ></Button>
        <Button
            android:id="@+id/button06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮6"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮7"      
        ></Button>
        <Button
            android:id="@+id/button08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮8"      
        ></Button>
        <Button
            android:id="@+id/button09"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮9"      
        ></Button>
    </TableRow>    
</TableLayout>

实际效果图如下:
这里写图片描述

该布局xml文件,有三行,每一行有3个Button按钮,是3 x 3的TableLayout。

2、TableLayout的android:shrinkColumns属性,当TableRow里边的空间布满布局的时候,指定列自动延伸以填充可用部分。当TableRow里边的控件还没有布满布局时,android:shrinkColumns不起作用。
下面的布局文件table_relative02_1.xml,演示了android:shrinkColumns属性的使用:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="2"
    >
    <TableRow >
        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1" 
        ></Button>
       <!-- android:text="按钮1AAAAAAAAAAAAAAA" --> 
        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2"      
        ></Button>
        <!-- android:text="按钮2AAAAAAAAAAAAAAA" --> 
        <Button
            android:id="@+id/button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3AAAAAAAAAAAAAAA"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"      
        ></Button>
        <Button
            android:id="@+id/button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"      
        ></Button>
        <Button
            android:id="@+id/button06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮6"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮7"      
        ></Button>
        <Button
            android:id="@+id/button08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮8"      
        ></Button>
        <Button
            android:id="@+id/button09"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮9"      
        ></Button>
    </TableRow>    
</TableLayout>

实际效果图如下:
这里写图片描述
从上面的实际效果图片可以看到,当TableLayout设置了android:shrinkColumns属性,则在TableRow中的控件如果超长的话,设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。

下面的布局文件table_relative02_2.xml,演示了没有设置android:shrinkColumns属性,则在TableRow中的控件超长,也不会自动延伸以填充可用部分。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TableRow >
        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1" 
        ></Button>
       <!-- android:text="按钮1AAAAAAAAAAAAAAA" --> 
        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2AAAAAAAAAAAAAAA"      
        ></Button>
        <!-- android:text="按钮2" --> 
        <Button
            android:id="@+id/button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3AAAAAAAAAAAAAAA"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"      
        ></Button>
        <Button
            android:id="@+id/button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"      
        ></Button>
        <Button
            android:id="@+id/button06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮6"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮7"      
        ></Button>
        <Button
            android:id="@+id/button08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮8"      
        ></Button>
        <Button
            android:id="@+id/button09"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮9"      
        ></Button>
    </TableRow>    
</TableLayout>

实际效果图如下:
这里写图片描述

3-1、TableLayout的android:stretchColumns属性,用于指定列对空白部分进行填充。
下面的布局文件table_relative03_1.xml,演示了android:stretchColumns属性的使用:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1"
    >
    <TableRow >
        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1" 
        ></Button>
        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2"      
        ></Button>
        <!-- android:text="按钮2" --> 
        <Button
            android:id="@+id/button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"      
        ></Button>
        <Button
            android:id="@+id/button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"      
        ></Button>
        <Button
            android:id="@+id/button06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮6"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮7"      
        ></Button>
        <Button
            android:id="@+id/button08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮8"      
        ></Button>
        <Button
            android:id="@+id/button09"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮9"      
        ></Button>
    </TableRow>    
</TableLayout>

实际效果图如下:
这里写图片描述

3-2、android:stretchColumns属性,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。
下面的布局文件table_relative03_2.xml,演示了多行android:stretchColumns属性的使用:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0,2"
    >
    <TableRow >
        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1" 
        ></Button>
        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2"      
        ></Button>
        <!-- android:text="按钮2" --> 
        <Button
            android:id="@+id/button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"      
        ></Button>
        <Button
            android:id="@+id/button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"      
        ></Button>
        <Button
            android:id="@+id/button06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮6"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮7"      
        ></Button>
        <Button
            android:id="@+id/button08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮8"      
        ></Button>
        <Button
            android:id="@+id/button09"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮9"      
        ></Button>
    </TableRow>    
</TableLayout>

实际效果图如下:
这里写图片描述

4、TableLayout的android:collapseColumns属性,用于隐藏指定的列,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。。
下面的布局文件table_relative04.xml,演示了android:collapseColumns属性的使用:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:collapseColumns="1"
    >
    <TableRow >
        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1" 
        ></Button>
        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2"      
        ></Button>
        <!-- android:text="按钮2" --> 
        <Button
            android:id="@+id/button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"      
        ></Button>
        <Button
            android:id="@+id/button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"      
        ></Button>
        <Button
            android:id="@+id/button06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮6"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮7"      
        ></Button>
        <Button
            android:id="@+id/button08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮8"      
        ></Button>
        <Button
            android:id="@+id/button09"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮9"      
        ></Button>
    </TableRow>    
</TableLayout>

实际效果图如下:
这里写图片描述

5、TableLayout的android:layout_span属性,设置组件显示所占用的列数。
下面的布局文件table_relative05.xml,演示了android:layout_span*属性的使用:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TableRow >
        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:text="按钮1" 
        ></Button>
        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2"      
        ></Button>
        <!-- android:text="按钮2" --> 
        <Button
            android:id="@+id/button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"      
        ></Button>
        <Button
            android:id="@+id/button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:text="按钮5"      
        ></Button>
        <Button
            android:id="@+id/button06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮6"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮7"      
        ></Button>
        <Button
            android:id="@+id/button08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮8"      
        ></Button>
        <Button
            android:id="@+id/button09"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮9"      
        ></Button>
    </TableRow>    
</TableLayout>

实际效果图如下:
这里写图片描述

6、TableLayout的android:layout_column属性,设置组件所在列数。
下面的布局文件table_relative06.xml,显示了android:layout_column属性的使用:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TableRow >
        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1" 
        ></Button>
        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="按钮2"      
        ></Button>
        <!-- android:text="按钮2" --> 
        <Button
            android:id="@+id/button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="按钮4"      
        ></Button>
        <Button
            android:id="@+id/button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"      
        ></Button>
        <Button
            android:id="@+id/button06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮6"      
        ></Button>
    </TableRow>

    <TableRow >
        <Button
            android:id="@+id/button07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮7"      
        ></Button>
        <Button
            android:id="@+id/button08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮8"      
        ></Button>
        <Button
            android:id="@+id/button09"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮9"      
        ></Button>
    </TableRow>    
</TableLayout>

实际效果图如下:
这里写图片描述

  • 13
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.1掌握Android四层体系架构 5 1.2 Eclipse工程文件 5 1.3 Android项目的编译及运行过程 6 2.1 AndroidStudio中R文件的位置 7 2.2 View继承结构图 7 2.3 LinearLayout布局基本属性 7 2.4 用户名密码 登陆重置常用代码 9 2.5 ARBG颜色 10 2.6 命名空间 10 2.7 RelativeLayout相对布局属性 10 2.8 相对布局代码 11 2.9 FramLayout 帧布局 13 2.10 TableLayout 表格布局 14 2.11 AbsoluteLayout绝对布局 16 3.1 sp、dp、dip、pt、px等单位的区别 17 3.2 TextView属性 18 3.3跑马灯效果的最小代码集 19 3.4给按钮注册点击事件的方式 19 3.5 EditText属性 20 3.6 simple_list_item_1是什么 21 3.7 ImageView的属性 22 3.8 CheckBox属性及相关代码 23 3.9 RadioGroup属性及相关代码 25 3.10 ToggleButton 26 3.11 Spinnner 28 3.12 DatePicker 29 3.13 TimePicker 30 4.1适配器是什么 30 4.2 ArrayAdapter的使用 32 4.3设置监听以及得到用户点中项的内容 32 4.4 AutoCompleteTextView的使用 33 4.5 Gallery核心代码及SimpleAdapter的使用 35 4.6 EditText设置监听 37 5.1 Activity生命周期 38 5.2 Activity中临时数据存储相关方法 40 5.3 Intent 意图的使用 43 5.4 使用显式意图激活组件的多种方式 44 5.5 Activity的六种传值方式 45 6.1 Task与BackStack概念 53 6.2 Activity的启动模式 54 6.3 Intent 六大属性 55 6.4 IntentFilter 意图过滤器 57 7.1 Android中进程的生命周期 61 7.2 UI线程模型的两条规则及矛盾解决的三种方法 61 7.3 方法一代码 62 7.4 AsyncTask 特点、参数及需要实现的方法 64 7.5 异步任务代码 64 7.6 下载进度对话框相关实现代码 67 7.7使用runONUiThread()\HttpURLConnection完成文件下载操作 68 7.8 掌握AsyncTask异步任务下载网络资源 70 7.9 DatePickerDialog、TimePickerDialog的使用 76 8.1 ListView、SimpleAdapter和ArrayAdapter的使用 78 8.2 自定义适配器及BaseAdapter 83 8.3 ListView的缓存原理 85 8.4 ListView配合AsyncTask加载网络数据——JSON/XML 87 9.1 数据分页策略及算法 106 9.2 分页加载代码 106 9.3解决图文混排的问题(方法1) 109 9.4 进度对话框提示加载和页脚提示加载 118 10.1 实现分页及解决图文混排的问题 119 10.2 GridView常用属性使用参考ListView) 124 10.3 ExpandableListView的使用 124 11.1选项菜单XML文件 132 11.2菜单的分类 132 11.3 选项菜单相关方法 133 11.4 JAVA代码生成选项菜单 134 11.5上下文菜单的编写步骤 135 11.6上下文菜单绑定到 ListView 代码 136 11.7弹出菜单的使用步骤 137 11.8 AlertDialog 常用方法 138 11.9 AlertDialog对话框的使用步骤 140 11.10 列表对话框使用 141 11.12 多选列表对话框 142 11.13 通知:简单、普通、大视图、带进度条、自定义通知 143 12.1 Fragment编写步骤 149 12.2 fragment生命周期演示 150 12.3ScrollView布局文件 156 12.4 Fragment代码 157 12.5 Activity传值到Fragment 159 12.6 Fragment传值到Activity 162 12.7 Fragment传值到Fragment 164 12.8万能的接口回调 165 13.1 Android数据存储分类 166 13.2 Shared Preferences存储 166 13.3 测试类的编写示例 170 13.4内部存储 173 13.5外部存储 176 14.1 SQL的分类 184 14.2常用SQL语句的使用 184 14.3数据库的使用(SQL) 185 14.4 数据库的使用(调用已封装SQL语句的方法) 200 14.5 简单游标适配器的使用及分页效果的实现 207 15.1对手机通讯录的增删改查 211 15.2查询手机通话记录 221 15.3操作手机短信 224 16.1 自定义内容提供者的编写步骤 226 16.2 如何使当前应用的内容提供者可以被其他应用访问到 236 17.1 Loader的使用步骤及SearchView的使用 237 17.2 AsyncTaskLoader的使用步骤以及它与CusorLoader的区别 245 18.1 使用Handler完成子线程发送消息和Runnable对象到主线程 250 18.2 使用Handler完成主线程发送消息到子线程 256 18.3内存泄露和内存溢出的区别以及引用的级别 260 18.4 使用软引用解决Handler内存泄漏问题 262 19.1 ActionBar的显示和隐藏 264 19.2 SearchView、ActionLayout、ShareActionProvider的使用 265 19.3 ActionBar选项卡模式的使用 270 19.4 ActionBar列表模式的使用 274 19.5通过功能清单文件设置主题 277 20.1ViewPager和PagerAdapter的使用 277 20.2 ViewPager 监听器的使用 281 20.3 带标题的ViewPager效果的实现 287 20.4 使用范例(UI) 291 20.5 FragmentPagerAdapter的使用 298 20.6 范例二 302 20.7 范例3 313 21.1 ActionBar、ViewPager及Fragment的混合使用 321 21.2检查手机网络状态的使用过程 327 21.3 WebView的使用 328 21.4 WebView+Jason 329 21.5 WebView访问服务器 331 21.6 VideoView的使用 336 22.1 广播的分类 338 22.2 正常广播 339 22.3 有序广播 341 22.4 粘滞广播 342 22.5 检查手机状态是否联网 344 22.6短信拦截 346 22.7 电话拦截 349 23.1启动服务 350 23.2 IntentService的使用 354 23.3 绑定服务 356 23.4 boundservicewithinterface 362 23.5启动式服务和绑定式服务的混合使用 370 23.6 手机窃听器的开发 371

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值