Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 表單輸入介面實作教學

編輯:Android 開發實例

本篇文章將帶大家實作一個典型的手機表單輸入介面。畫面(也就是 Activity 的窗體)在視覺上可切分為三個區塊:

  • 頂部:標題與資訊提示(Title)。
  • 中間:可捲動的表單內容區。
  • 底部:操作按鈕(例如 Save)。

用到的核心元件包括:RelativeLayout(相對佈局)、ScrollView(捲動視圖)以及 TableLayout(表格佈局)。

一、用 RelativeLayout 搭建三區塊骨架

要實現「頂部固定、底部固定、中間自動填滿」的效果,RelativeLayout 是最合適的選擇。步驟如下:

  1. 在根視圖放置一個 RelativeLayout 作為根容器,並指定 fill_parent 填滿整個視窗。
  2. 在根容器中放入三個子元件,分別對應頂部、中間、底部。
  3. 分別設定這三個元件的佈局屬性(對齊方式)。

關鍵屬性說明:

  • 頂部元件:android:layout_alignParentTop="true",對齊父容器的頂端。
  • 底部元件:android:layout_alignParentBottom="true",對齊父容器的底端。
  • 中間元件:android:layout_below="@id/toppanel" 表示位於頂部元件下方;android:layout_above="@id/panelBottom" 表示位於底部元件上方。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/toppanel">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="用戶資訊表單(頂部)"
            android:textSize="11pt" />
    </RelativeLayout>

    <RelativeLayout android:id="@+id/panelBottom"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content">
        <Button
            android:text="Save(底部元件)"
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent" />
    </RelativeLayout>

    <ScrollView
        android:layout_below="@id/toppanel"
        android:layout_above="@id/panelBottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <!-- 這裡放置中間部分的元件 -->
    </ScrollView>
</RelativeLayout>

上述程式碼中:

  • 頂部容器 RelativeLayout 命名為 toppanel。
  • 底部容器 RelativeLayout 命名為 panelBottom。
  • 中間使用 ScrollView 捲動視圖,好處是當子元件內容過長時會自動出現捲動條。

二、在 ScrollView 中加入 TableLayout

接著在中間的 ScrollView 內加入一個 TableLayout 表格佈局,讓表單排列更整齊。

<TableLayout android:padding="3dip"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:stretchColumns="1"
    android:layout_height="fill_parent">

    <TableRow>
        <TextView android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="right"
            android:padding="3dip"
            android:text="User">
        </TextView>
        <EditText android:id="@+id/editText1"
            android:padding="3dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></EditText>
    </TableRow>
</TableLayout>

其中 android:stretchColumns="1" 讓第二欄(索引從 0 起算)自動伸展填滿剩餘空間,使得輸入框能佔滿整行,畫面更為美觀。

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