Android--(8)--详解表格布局(TableLayout)

TableLayout布局的特点:没加入一个TableRow就表示表格添加一行,然后TableRow中每添加一个控件就表示该行加入一列;TableRow中的空间不能指定宽度;   
在表格布局中:

  • 表格的列数由控件最多的一行决定;

  • 列宽由控件最宽的一个决定;

常见的几个属性:

  • android:layout_span=”2”     合并两个单元格;

  • android:stretchColumns=”1”     拉伸第二列:

  • android:shrinkColumns=”2”          收缩第三列:

  • android:collapseColumns=”0”      第0列隐藏(设置某列隐藏:)

实例:实现列的隐藏:
先贴图:
初始状态

点击效果
上代码:
activity_main.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/table1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1"
    tools:context="com.example.tablelayoutdemo.MainActivity" >

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="50dp" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_column="1"
            android:gravity="center_vertical"
            android:text="打开···" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="Ctrl+o" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_column="1"
            android:gravity="center_vertical"
            android:text="保存···" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="Ctrl+s" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_column="1"
            android:gravity="center_vertical"
            android:text="另保存···" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="Ctrl+Shift+s" />
    </TableRow>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#686868" />

    <TableRow>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="*" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_column="1"
            android:gravity="center_vertical"
            android:text="导入···" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="*" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_column="1"
            android:gravity="center_vertical"
            android:text="导出···" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="Ctrl+E" />
    </TableRow>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#686868" />

    <TableRow>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_column="1"
            android:gravity="center_vertical"
            android:text="退出" />
    </TableRow>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="隐藏" />

</TableLayout>

MainActivity.java中的主要代码:

    //声明一下控件名称:
    Button button;
    TableLayout t1;
    boolean falg=true;
    boolean falgs=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button) findViewById(R.id.button);
        t1=(TableLayout) findViewById(R.id.table1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(falg){
                    //setColumnCollapsed(0, true):设置某列是否为为隐藏状态
                    t1.setColumnCollapsed(0, true);
                    button.setText("显示");
                    falg=false;
                }else{                  
                    t1.setColumnCollapsed(0, false);
                    button.setText("隐藏");
                    falg=true;
                }
            }
        });
    }

//学习使用,代替笔记!!!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值