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

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

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

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

常见的几个属性:ide

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

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

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

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

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

点击效果
上代码:
activity_main.xml: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;
                }
            }
        });
    }

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