美文网首页
表格布局(TableLayout)

表格布局(TableLayout)

作者: Fastcv | 来源:发表于2019-07-03 23:10 被阅读0次

前言

表格布局继承自LinearLayout,通过TableRow设置行,列数由TableRow中的子控件决定,直接在TableLayout中添加子控件会占据整个一行。(结合大佬们的博客总结的,如有侵权,麻烦联系我删除此文章)

如何确定行数与列数

1、如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!!

2、如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!

3、tablerow中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定tablerow的layout_width属性,默认是fill_parent的,我们自己设置成其他的值也不会生效!!! 但是layout_height默认是wrapten——content的,我们却可以自己设置大小! 整个表格布局的宽度取决于父容器的宽度(占满父容器本身)

4、有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数

属性

常用属性:

属性 说明
android:shrinkColumns 设置可收缩的列,内容过多就收缩显示到第二行
android:stretchColumns 设置可伸展的列,将空白区域填充满整个列
android:collapseColumns 设置要隐藏的列

列的索引从0开始,shrinkColumns和stretchColumns可以同时设置。

子控件常用属性:

属性 说明
android:layout_column 第几列
android:layout_span 占据列数

举例

tablelayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF"
    android:orientation="vertical"
    tools:context="com.xiaohei.auser.myanimation.Main2Activity">

    <TableLayout
        android:layout_width="match_parent"
        android:collapseColumns="1"
        android:layout_height="wrap_content">
        <TableRow>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="one" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="two" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="three" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="four" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:stretchColumns="1"
        android:layout_height="wrap_content">
        <TableRow>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="one" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="two" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="three" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="four" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:shrinkColumns="3,4"
        android:layout_height="wrap_content">
        <TableRow>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="one" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="two" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="three" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="four" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="four" />
        </TableRow>
    </TableLayout>
</LinearLayout>

示意图:


tablelayout.png

附加

相关文章

网友评论

      本文标题:表格布局(TableLayout)

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