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;
                }
            }
        });
    }

//學習使用,代替筆記!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章