天天看點

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

TableLayout布局的特點:沒加入一個TableRow就表示表格添加一行,然後TableRow中每添加一個控件就表示該行加入一列;TableRow中的空間不能指定寬度;   

在表格布局中:

  • 表格的列數由控件最多的一行決定;
  • 列寬由控件最寬的一個決定;

常見的幾個屬性:

  • android:layout_span=”2”     合并兩個單元格;
  • android:stretchColumns=”1”     拉伸第二列:
  • android:shrinkColumns=”2”          收縮第三列:
  • android:collapseColumns=”0”      第0列隐藏(設定某列隐藏:)

執行個體:實作列的隐藏:

先貼圖:

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

上代碼:

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(, true);
                    button.setText("顯示");
                    falg=false;
                }else{                  
                    t1.setColumnCollapsed(, false);
                    button.setText("隐藏");
                    falg=true;
                }
            }
        });
    }
           

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

繼續閱讀