表格布局(TableLayout)

Tablelayout以行和列的形式对控件进行管理,每一行为一个TableRow对象。当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。
有多少个子控件就有多少列;当为View时,该View将独占一行。

按照国际惯例先上布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="C" />

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="÷" />
                </TableRow>
            </TableLayout>

            <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="back" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="7" />

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="8" />
                </TableRow>
            </TableLayout>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="9" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="+" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="4" />

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

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="6" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="-" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="1" />

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="2" />
                </TableRow>

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"></TableRow>

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="0" />
            </TableLayout>

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="3" />
                </TableRow>

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="." />
                </TableRow>
            </TableLayout>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@android:color/holo_orange_dark"
                android:text="=" />
        </TableRow>
    </TableLayout>
</RelativeLayout>
效果.png

大家也看到了,这个效果为一个计算器的布局,为什么要选择计算器的布局呢?因为使用计算器的布局能基本上把TableLayout的所有特性都表达清楚。
因为这里面既包含了行合并有包含了列合并

  • 列合并的时候需要对应的列都按照合并的个数来对应书写
    例如:计算器中“0”需要列合并,那么“C”和“÷”、“7”和“8”、“4”和“5”、“1”和“2”就需要写在一个TableLayout的2个TableRow里面,这样到后面“0”才会占有2个元素的空间
  • 行合并时也需要对应的行都按照合并的个数来对应书写
    例如:计算器中“=”需要列合并,那么“1”、“2”、“3”、“0”、“.”、“=”就需要写在一个TableRow里面,然后在横向分为3部分:
    1. 第一部分 “1”、“2”、“0”
    2. 第二部分 “3”、“.”
    3. 第三部分 “=”

下面为布局结构简图

<TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TableLayout ...>
            <TableLayout ...>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@android:color/holo_orange_dark"
                android:text="=" />
        </TableRow>

TableLayout只要能熟练掌握这个计算器的布局,那么在平时开发中应该算是完全可以胜任了。
对了在design包中还有一个和TableLayout名字和读音很相似的的一个布局,TabLayout 但是用法和功能完全不同,下次详细介绍这个。

代码只会按照你所写的方式运行,不会按照你想的方式运行

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,747评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,549评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,216评论 4 61
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,686评论 1 92
  • 《教育的本质》
    dq920813阅读 2,118评论 0 0