Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

從零開始學 Android(十五):TableLayout 表格布局詳解

編輯:關於 Android 程式設計

TableLayout 的模型很直白:把容器劃分成一列一列(行),每一行裡可以放多個控件,形成表格狀的排布。它還支援設置控件在行內的對齊方式,以及標記某一行是否為「可收縮」,讓內容過寬時能自動壓縮。

一、用 XML 靜態配置

TableLayout 中的每一行由 TableRow 表示,未被子元素填滿的行不會自動補位。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1">

    <TableRow>
        <TextView android:id="@+id/text01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用戶名" />
        <EditText android:id="@+id/edit01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10">
            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow>
        <TextView android:id="@+id/text02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密 碼" />
        <EditText android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPassword" />
    </TableRow>

    <TableRow>
        <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="登錄系統" />
    </TableRow>

</TableLayout>

從運行效果可以清楚看到:第一行、第二行各自排了兩個控件,第三行只有一個按鈕,並透過 layout_gravity 達到了水平居中。

二、用代碼動態構建同樣的界面

package com.example.tablelayout;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TableLayout layout = new TableLayout(this);
        TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        // 三行
        TableRow row1 = new TableRow(this);
        TableRow row2 = new TableRow(this);
        TableRow row3 = new TableRow(this);

        // 第一行:用戶名
        TextView tv1 = new TextView(this);
        tv1.setText("用戶名");
        EditText et1 = new EditText(this);
        row1.addView(tv1);
        row1.addView(et1);

        // 第二行:密碼
        TextView tv2 = new TextView(this);
        tv2.setText("密 碼");
        EditText et2 = new EditText(this);
        row2.addView(tv2);
        row2.addView(et2);

        // 第三行:居中按鈕
        Button button = new Button(this);
        button.setText("登錄系統");
        row3.setGravity(Gravity.CENTER);
        row3.addView(button);

        layout.addView(row1);
        layout.addView(row2);
        layout.addView(row3);

        setContentView(layout, tableParams);
    }
}

三、兩種方式該怎麼選

動態布局能實現與 XML 完全一致的視覺效果,但代碼冗長、可讀性差,而且圖文混排時難以直觀調整。實務上的建議是:界面結構相對固定就用 XML,只有當行數、列數需要依數據動態變化時,才動用代碼生成。

另外兩個常用屬性值得記住:android:stretchColumns 指定哪些列可以拉伸填滿剩餘寬度,android:shrinkColumns 則指定哪些列在內容溢出時可以收縮。

下節預告:相對布局 RelativeLayout。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved