Android TableLayout记

tablelayout是一种Android UI布局,继承自linearLayout,
一行中添加单个组件:直接在TableLayout中添加组件
一行中添加多个组件:使用TableRow,将组件写在TableRow下
TableLayout 中有多少个组件,就有多少行    (一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数)

常用属性:
tableLayout下
android:collapseColumns:设置需要被隐藏的列的序号
android:shrinkColumns:设置允许被收缩的列的列序号
android:stretchColumns:设置运行被拉伸的列的列序号(可沾满剩余空间)
android:layout_column="2":表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的!
android:layout_span="4":表示合并4个单元格,也就说这个组件占4个单元格
TableRow的子控件的主要属性:
android:layout_column=”1” 该控件显示在第1列
android:layout_span=”2” 该控件占据2


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TableLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/pink">
        <Button
                android:layout_width="match_parent"
                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="1-1"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-2"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-3"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1-4"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-5"/>
        </TableRow>
    </TableLayout>
    
    <TableLayout
            android:background="@color/hui"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:collapseColumns="2">
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="属性1:android:collapseColumns:需要被隐藏的列序号"/>
        <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-1"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-2"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-3"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-4"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-5"/>
        </TableRow>
    </TableLayout>

    <TableLayout
            android:background="@color/blue"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:shrinkColumns="2">
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="属性2:android:shrinkColumns:允许被收缩的列序号"/>
        <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-1"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-2"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-3"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-4"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-5"/>
        </TableRow>
    </TableLayout>

    <TableLayout
            android:background="@color/green"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:stretchColumns="1">
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="属性3:android:stretchColumns:允许被拉伸的列序号"/>
        <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-1"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-2"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-3"/>
        </TableRow>
    </TableLayout>

   <TableLayout
            android:background="@color/yellow"
            android:layout_width="match_parent"
            android:layout_height="300dp">
        <Button
                android:layout_width="match_parent"
                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="1-1"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-2"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-3"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-4"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1-5"/>
        </TableRow>
       <Button
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="子控件属性:android:layout_column=”1”控件显示在第1列"/>
        <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2-1"/>
            <Button
                    android:layout_column="3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2-2"/>
            <Button

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2-3"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2-4"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2-5"/>
        </TableRow>
       <Button
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="子控件属性:android:layout_span=”2” 该控件占据2列"/>
        <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3-1"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3-2"/>
            <Button
                    android:layout_span="2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3-3"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3-4"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3-5"/>
        </TableRow>
        <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4-1"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4-2"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4-3"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4-4"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4-5"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值