編輯:進階開發
使用者介面最基本的建材是 View。一個 View 物件佔據螢幕上的一塊矩形區域,負責把自己畫出來並處理觸發於其上的事件。Button、TextView 這些常見元件,全都是 View 的子類別。
ViewGroup 則是 View 的子類別,本身不顯示任何內容,作用是作為不可見的容器,用來裝載其他 View 或其他 ViewGroup,並決定它們的配置方式。
而在第三層,不同的「布局」正是 ViewGroup 的各個子類別。一個典型的 Android 介面就是由這些布局定義出來的,建立方式有兩種:在執行期用 View/ViewGroup 物件組出可視結構,或者用 XML 檔(例如專案 res/layout 目錄下的 main_layout.xml)宣告式地描述。本文聚焦於後者,因為 XML 定義介面更直覺、也更容易維護。
布局檔案裡可以放任何類型的元件——按鈕、標籤、文字框等等。以下是一個最簡單的 LinearLayout 例子:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a TextView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a Button" /> <!-- More GUI components go here --> </LinearLayout>
布局寫好之後,就能在 Activity 的 onCreate() 回呼中把它載入畫面:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
Android 內建了多種布局,幾乎涵蓋所有常見的畫面結構需求,各有不同的排列邏輯與外觀風格:
| 布局 | 說明 |
|---|---|
| Linear Layout | 把子元件沿單一方向排列,可選垂直或水平。 |
| Relative Layout | 依相對位置安排子元件,可指定相對於父容器或其他兄弟元件。 |
| Table Layout | 把子元件劃分成列與欄的表格結構。 |
| Absolute Layout | 以絕對座標指定每個子元件的精確位置。 |
| Frame Layout | 螢幕上的一個佔位符,通常用來容納單一檢視。 |
| List View | 顯示可捲動的項目清單。 |
| Grid View | 以二維網格呈現可捲動的項目群組。 |
每種布局都有一組屬性用來描述視覺外觀。有些屬性在各種布局之間通用,以下是最常用的一批:
| 屬性 | 說明 |
|---|---|
| android:id | 為該元件指定唯一識別碼 |
| android:layout_width | 布局寬度 |
| android:layout_height | 布局高度 |
| android:layout_marginTop | 頂側外距 |
| android:layout_marginBottom | 底側外距 |
| android:layout_marginLeft | 左側外距 |
| android:layout_marginRight | 右側外距 |
| android:layout_gravity | 指定子元件在容器中的位置 |
| android:layout_weight | 指定配置多餘空間時各元件佔的比例 |
| android:layout_x | 配置的 x 座標 |
| android:layout_y | 配置的 y 座標 |
| android:paddingLeft | 左側內距 |
| android:paddingRight | 右側內距 |
| android:paddingTop | 頂部內距 |
| android:paddingBottom | 底部內距 |
寬度與高度可以用幾種單位指定:dp(密度獨立像素)、sp(比例獨立像素)、pt(點,1/72 吋)、px(像素)、mm(毫米)與 in(英吋)。實務上為了讓介面在不同解析度下都能正常呈現,dp 與 sp 是最常用的選擇。
雖然可以寫死精確數值,但更常見的做法是使用這兩個常量:
Gravity 屬性在決定元件位置時扮演關鍵角色。它可以接受一個或多個(以「|」分隔)下列常量:
| 常量 | 值 | 說明 |
|---|---|---|
| top | 0x30 | 推向容器頂端,不改變尺寸 |
| bottom | 0x50 | 推向容器底端,不改變尺寸 |
| left | 0x03 | 推向容器左側,不改變尺寸 |
| right | 0x05 | 推向容器右側,不改變尺寸 |
| center_vertical | 0x10 | 置於容器垂直中心,不改變尺寸 |
| fill_vertical | 0x70 | 必要時拉長垂直尺寸以填滿容器 |
| center_horizontal | 0x01 | 置於容器水平中心,不改變尺寸 |
| fill_horizontal | 0x07 | 必要時拉長水平尺寸以填滿容器 |
| center | 0x11 | 置於容器水平與垂直雙軸中心,不改變尺寸 |
| fill | 0x77 | 必要時同時填滿水平與垂直方向 |
| clip_vertical | 0x80 | 讓子元件頂部或底部邊緣裁切貼齊容器邊界,究竟裁哪一側取決於搭配的垂直重力 |
| clip_horizontal | 0x08 | 讓子元件左側或右側邊緣裁切貼齊容器邊界,裁哪一側取決於搭配的水平重力 |
| start | 0x00800003 | 推向容器起點,不改變尺寸 |
| end | 0x00800005 | 推向容器終點,不改變尺寸 |
每個 View 都可以配上一個唯一的 ID,供後續在程式碼中辨識使用。在 XML 標籤中的寫法如下:
android:id="@+id/my_button"
這裡的 @+ 兩個符號各有意義:
有了 ID,就能在程式碼中取得該 View 的實例並加以操作:
Button myButton = (Button) findViewById(R.id.my_button);
Android 2.0 全面解析:新 API、Eclair 與 G1 的三方合作
Android 智慧手機為開發者帶來了一系列新 API,包含同步與藍牙等功能。對開發人員而言,Android 2.0 平台可作為 SDK 的一部分下載,內容涵蓋完整的 A
解析Android應用程式的構成原理:為何只能用Java
Android應用程式以Java開發,而且幾乎只能用Java開發。Google對Android採取的是一種全面掌控式的管理,與其他開源軟體的協作模式完全不同,絕大部分決定
Android 平台預計占據 HTC 明年半數產品
HTC Hero 把 Android 介面徹底改頭換面,變得如此美觀而更具吸引力。可以預估,到了 2010 年,大部分新的 Android 平台機種都會換上與 Hero
多彩的 Android 手機系統:從 Dalvik 到應用市場的產業鏈效應
Dalvik 虛擬機器依賴 Linux 的一些特性,例如執行緒機制與底層記憶體管理;Linux 核心同時充當硬體與軟體堆疊之間的硬體抽象層。理解這一層關係,大致就掌握了