美文网首页
六、表格布局TableLayout

六、表格布局TableLayout

作者: 清梦星河哈 | 来源:发表于2019-07-01 10:52 被阅读0次

表格布局由TableLayout所代表,表格布局采用行、列的形式来管理UI组件,TableLayout并不需要明确的声明包含多少行、多少列,而是添加TableRow、其他组件来控制表格的行数和列数。

每次向TableLayout中添加一个TableRow,该TableRow就是一个表格行,TableRow也是容器,因此它也可以不断的添加其他控件,每添加一个子控件该表格就增加一列。

如果直接向TableLayout中添加组件,那么这个组件将直接占用一行。

在表格布局中,列的宽度由该列中最宽的那个单元格决定,整个表格布局的宽度则取决于父容器的宽度(默认总是占满父容器的本身)。

在表格布局管理器中,可以为单元格设置如下三种行为方式:
1.Shrinkable:如果某个列被设置为Shrinkable,那么该列的所有单元格的宽度可以被收缩,以保证该单元格能适应父容器的宽度。
2.Stretchable:如果某个列被设置为Stretchable,那么该列的所有单元格的宽度可以被拉伸,以保证组件能完全填满表格空余空间。
3.Collapse:如果某个列被设置为Collapse,那么该列的所有单元格会被隐藏。

TableLayout的常用XML属性及相关方法

XML属性 相关方法 说  明
android:collapseColumns setColumncollapsed(int, boolean) 设置需要被隐藏的列的列序号,多个列序号之间用逗号隔开
android:shrinkColumns setShrinkAllColumns(boolean) 设置允许被收缩的列的列序号,多个列序号之间用逗号隔开
android:stretchColumns setStretchAllColumns(boolean) 设置允许被拉伸的列的列序号,多个列序号之间用逗号隔开

收缩拉伸例子

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:stretchColumns="2">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="独自一行的按钮"/>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="允许被收缩的按钮"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="允许被拉伸的按钮"/>
        </TableRow>
    </TableLayout>

效果如图:


TableLayout.jpg

隐藏例子

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="独自一行的按钮"/>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第一个按钮"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第二个按钮"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第三个按钮"/>
        </TableRow>
    </TableLayout>

效果如图:


TableLayout.jpg

相关文章

  • Android 常见布局

    基本理论 Android六大基本布局分别是:线性布局LinearLayout、表格布局TableLayout、相对...

  • Android之6大布局

    LineLayout (线性布局) RelativeLayout(相对布局) TableLayout(表格布局) ...

  • 2 布局

    LinearLayout(线性布局) RelativeLayout(相对布局) TableLayout(表格布局)...

  • 六、表格布局TableLayout

    表格布局由TableLayout所代表,表格布局采用行、列的形式来管理UI组件,TableLayout并不需要明确...

  • 零基础学鸿蒙编程-UI控件_TableLayout

    什么是TableLayout TableLayout又称表格布局,用于以表格形式展示内容. 1. 样例:2*2表格...

  • Android学习笔记

    TableLayout 表格布局 AbsoulteLayout 绝对布局 FrameLayout 帧布局 Rela...

  • 基础布局

    Android中的布局 线性布局:LinerLayout 表格布局:TableLayout 相对布局:Relati...

  • 2017年常见android面试题

    五大布局: LinearLayout线性布局FrameLayout层叠布局TableLayout 表格布局Abso...

  • Android学习笔记

    一、布局方式: (1)线性布局LinearLayout(2)表格布局TableLayout(3)帧布局FrameL...

  • TableLayout-表格布局

    表格布局让我想到了GridLayout(网格布局),TableLayout为表格布局,也是本章的重点所在。 Tab...

网友评论

      本文标题:六、表格布局TableLayout

      本文链接:https://www.haomeiwen.com/subject/xhyecctx.html