2.2.4 表格布局

表格布局的概念

表格布局(TableLayout)是以表格形式排列控件的,通过行和列将界面划分为多个单元格,每个单元格都可以添加控件。

表格布局需要和TableRow配合使用,每一行都由TableRow对象组成,因此TableRow的数量决定表格的行数。
而表格的列数是由包含最多控件的TableRow决定的,例如第1个TableRow有两个控件,第2个TableRow有三个控件,则表格列数为3。

基本属性


1 TableLayout属性

属性 含义
android:layout_stretchColumns 设置被拉伸的列,从0开始
android:layout_shrinkColumns 设置被收缩的列,从0开始
android:layout_collapsecolumns 设置该列被隐藏,从0开始

例如:设置一个两行三列的表格,第三列为拉伸列,当三列不能排完一行时,第三列进行拉伸。

<?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="2"
    >
<TableRow>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="1"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="2"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="3"/>

</TableRow>
    <TableRow>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="4"/>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="5"
            />
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="6"
            />
    </TableRow>
</TableLayout>

2 TableLayout控件属性

属性 含义
android:layout_column 设置单元格显示位置,从0开始
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"
    android:stretchColumns="2"
    >
<TableRow>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="1"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="2"/>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="3"/>

</TableRow>
    <TableRow>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="4"/>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="5"
            android:layout_span="2"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="6"
            android:layout_column="1"
            />
    </TableRow>
</TableLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,721评论 0 17
  • 一.四大组件: Android四大组件分别为activity、service、content provider、b...
    Ludiwgbet阅读 5,661评论 0 39
  • 看了几篇文章以及自己写了一些,关于布局的问题,根据别人的写作总结一下得到 一.基本理论Android六大基本布局分...
    shuaikun阅读 4,208评论 0 4
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,327评论 25 709
  • 寥落豪客远西经,黯淡红梅嗅雨晴。 一望玉门弹指老,烟花大漠碎残星。 酒酣欢畅慰浮生,仗剑朦胧月下乘。 年少不知狂放...
    寸君阅读 3,157评论 2 2