Android UI佈局之TableLayout

從字面上瞭解TableLayout是一種表格式的佈局。這種佈局會把包含的元素以行和列的形式進行排列。表格的列數爲每一行的最大列數。當然表格裏邊的單元格是可以爲空的。

實例:LayoutDemo
運行效果:


代碼清單:
佈局文件:table_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1"
    >
    <TableRow>
        <TextView
            android:gravity="right"
            android:textStyle="bold"
            android:padding="3dip"
            android:text="用戶名稱:"
        />
        <EditText 
            android:id="@+id/username"
            android:padding="3dip"
            android:scrollHorizontally="true"
        />
    </TableRow>
    <TableRow>
        <TextView
            android:gravity="right"
            android:textStyle="bold"
            android:padding="3dip"
            android:text="用戶密碼:"
        />

        <EditText 
            android:id="@+id/password"
            android:padding="3dip"
            android:password="true"
        />
    </TableRow>
    <TableRow android:gravity="right">
        <Button
            android:id="@+id/cancel"
            android:text="取消" 
        />
        <Button
            android:id="@+id/login"
            android:text="登錄" 
        />
    </TableRow>
</TableLayout>
在上面的佈局代碼中一共有3行,即3個TableRow,每一個TableRow裏邊都有兩個單元格。
TableLayout標籤定義了一個表格佈局(TableLayout).
TableRow標籤定義了表格佈局裏邊的一行。每一行裏邊可以自由的加入一些組件,比如在上邊我們主要添加了按鈕組件和編輯框組件。

Java源代碼文件:TableLayoutActivity.java
package com.rainsong.layoutdemo;

import android.app.Activity;
import android.os.Bundle;

public class TableLayoutActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_layout);
    }
}



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