國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
12
事情。應用程式並不會因為 Broadcast Receivers 被呼叫而被它執行起來,而是當 Broadcast Receiver 因接收信息而觸發時系統會依宣告的規畫執行相對應的應用程式,應用程式也 可以利用 Context.sendBroadcast()方法來發出自己的 intent broadcast 給其他的應程式。
意圖(Intent)
這個功能常用於切換頁面 Activity 時使用,但除了切換頁面外,Intent 也會依照所提供 的內容不同,自動開啟對應的程式來檢視內容(例如開啟網頁和開啟地圖等)。當創建了 許多 Activities 之後,該如何讓這些 Activities 切換,我們可以利用 Intent 這個類別。Intent 類別主要是描述應用的功能,描述的結構為動作和動作相對應的資料,當我們在應用程 式中有需要使用到 Intent 時,則必須在 AndroidManifest.xml 檔案裡的 IntentFilter 中描 述,利用 IntentFilter 裡的描述可以透過比對找到應用程式需要呼叫的 Activity 是哪一個,
藉由 android.intent.category.LAUNCHER 來觸發。Activity 的切換會經過以下幾個步驟:
首先當系統中正在執行的 Activity(在此稱 Activity_01)切換至另一個 Activity(在此稱 Activity_02)時,系統會先暫停 Activity_01,然後建立 Activity_02 並執行其生命週期。當 被切換的 Activity_01 過了一段時間仍未執行時,系統會先停止 Activity_01 的狀態並回 收 Activity_01 所佔據的資源,上述對應的 Activity 狀態流程為圖 5-4。
2.3 Android 佈局介紹
本節將針對 Android 程式中常用的五大佈局介紹。圖 2-2 為佈局(Layout)的繼承關係圖,
深色區塊為 Android 五大佈局,分別為線性佈局、表格佈局、相對佈局、框架佈局和絕 對佈局。以下各小節分別介紹各個佈局,說明其常用屬性、並列出對應的 XML 程式範 例以及範例圖片。一旦開發人員了解 Android 在 XML 上對佈局的描述之後,在布局底 下的元件就會依據所選用的佈局而有相對應的顯示。
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
13
ViewGroup
LinearLayout RelativeLayout FrameLayout
TableLayout
AbsoluteLayout View
圖 2-2:Layout 的繼承關係圖。
2.3.1 線性佈局(LinearLayout)
Android 的元件佈局是以 Layout 作為包覆的容器,並可在容器中進行元件的排版。元件 佈局中最常用 LinearLayout 做為應用程式的排版樣式。表 2-1 提供了 LinearLayout 常用 的屬性,並以 XML 來實作對應屬性的程式碼(如表 2-2 所示),可藉由 XML 程式定義 元件的位置、大小、顏色與排版等,最後完成使用者介面(如圖 2-3 所示),使用水平 (Horizontal)屬性後,各元件排列的方式為從左至右;使用垂直(Vertical)屬性後,各元件 排列的方式則為從上至下。
表 2-1: LinearLayou 的屬性和說明。
屬性 說明
android:id 該屬性指定一個辨識符號給元件,並自動在 R.java 中建立索引,透過此 id 可用來調用元件。
android:layout_width android:layout_height
該屬性定義元件的寬度和高度,可使用的屬性值 有”fill_parent”(填滿容器空間)、”wrap_content”(根據 內部內容延展至適當大小)、”match_parent”(大致上 功能與”fill_parent”相同,2.2 版本才可使用)。
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
14
表 2-2:Android 上實作 LinearLayout 元件佈局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_weight="fill_parent"
android:layout_height="fill_parent" >
<Button android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="Button1" />
<Button android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:text="Button2" />
<Button android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn3"
android:text="Button3" />
<Button android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn4"
android:text="Button4" /></LinearLayout>
圖 2-3:LinearLayout。
2.3.2 表格佈局(TableLayout)
它可以把畫面切割為表格,使用者可以依照設計需求,把畫面切割為像是 2X4 的區塊。 每一個區塊都可以放一個 GUI 元件,如果需要的畫面不是一個固定的表格,也可以調整 它們的區塊。這樣的畫面配置需使用「TableLayout」標籤,搭配「TableRow」標籤建立 所需的表格。TableLayout 會控制畫面元件的寬與高,所以在這個標籤中的畫面元件都不
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
15
需要設定寬與高,就算加入設定也不會有效果。表 2-3 提供了 TableLayout 與 TableRow 常用的屬性,並以 XML 來實作對應屬性的程式碼(如表 2-4 所示),最後完成使用者介 面(如圖 2-4 所示)。
表 2-3:Tablelayout 與 TableRow 的屬性和說明。
Tablelayout 的屬性 說明
android:stretchColumns 指定欄位填滿剩餘的空間,欄位索引從 0 開始 android:shrinkColumns 縮小指定欄位的空間,欄位索引從 0 開始 android:collapseColumns 刪除指定欄位,欄位索引從 0 開始
TableRow 的屬性 說明
android:layout_span 擴展該攔位的格數,包括本身欄位 android:layout_column 用來標註該攔的索引
表 2-4:Android 上實作 TableLayout 元件佈局
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1,2" >
<TableRow> <Button android:text="Button1" />
<Button android:text="Button2"
android:layout_span="2" /> </TableRow>
</TableLayout>
圖 2-4:TableLayout。
2.3.3 相對佈局(RelativeLayout)
由於應用程式需要的畫面可能不會都是很規則的排列,如果畫面元件的位置與排列比較 複雜與不規則的時候,就比較適合使用 RelativeLayout 這種排列方式。它可以讓使用者 決定元件在畫面上位置,以及設定元件與元件之間的相關位置與對齊方式。這種比較靈
‧
表 2-5:RelativeLayout 中的屬性和說明。
屬性 說明
android:layout_above 將此元件置於"指定元件"上方.
android:layout_below 將此元件置於"指定元件"下方.
android:layout_toLeftOf 將此元件置於"指定元件"左方.
android:layout_toRightOf 將此元件置於"指定元件"右方.
android:layout_alignParentTop 將此元件對齊於佈局畫面上邊線 android:layout_alignParentRight 將此元件對齊於佈局畫面右邊線 android:layout_alignParentLeft 將此元件對齊於佈局畫面左邊線 android:layout_alignParentBottom 將此元件對齊於佈局畫面底線
表 2-6:Android 上實作 RelativeLayout 元件佈局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="fill_parent"
android:layout_height="fill_parent" >
<Button android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button1"
android:text="Button1" />
<Button android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button2"
android:text="Button2"
android:layout_below="@+id/Button1"
android:layout_toRightOf="@+id/Button1" />
<Button android:layout_weight="wrap_content"
android:layout_height="wrap_content"
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
17
圖 2-5:RelativeLayout。
2.3.4 框架佈局(FrameLayout)
為螢幕上的一個空白備用區域,該佈局可用在特定區塊中顯示單一元件項目,當該佈局 有多個元件需呈現時,會以堆疊方式進行呈現。因此元件呈現的順序會以堆疊最上層的 元件為主。如表 2-8 的範例,當兩個元件大小相同或上層元件大於下層元件時,下層的 元件就會被上層元件覆蓋,因此本範例中的下層元件被中層元件給覆蓋了,而中層元件 一部分被上層元件覆蓋,所以使用此佈局方式要注意覆蓋的問題。
表 2-7 提供了 FrameLayout 常用的屬性,並以 XML 來實作對應屬性的程式碼(如表 2-8 所示),最後完成使用者介面(如圖 2-6 所示)。
表 2-7:FrameLayout 中的屬性和說明。
屬性 說明
android:layout_width android:layout_height
該屬性定義元件的寬度和高度,可使用的屬性值 有"fill_parent"(填滿容器空間)、"wrap_content"(根 據內部內容延展至適當大小)、"match_parent"(大 致上功能與"fill_parent"相同,2.2 版本才可使用).
表 2-8:Android 上實作 FrameLayout 元件佈局。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation=" vertical"
android:layout_weight="fill_parent"
android:layout_height="fill_parent" >
<Button android:layout_weight="wrap_content"
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
18
android:layout_height="wrap_content"
android:text="下層元件" />
<Button android:layout_weight=" fill_parent "
android:layout_height="wrap_content"
android:text="中層元件" />
<Button android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:text="上層元件" />
</FrameLayout>
圖 2-6:FrameLayout。
2.3.5 絕對佈局(Absolutelayout)
可以讓元件根據指定的 X 和 Y 座標值顯示於螢幕上,起始左上角為(X,Y)座標為(0,0),
向右或向下移動時數值增加,由於 AbsoluteLayout 是以沒有外框的概念設計,因此元件 有可能超出螢幕或是互相重疊,不易在不同螢幕尺寸的環境下使用。表 2-9 提供了 FrameLayout 常用的屬性,並以 XML 來實作對應屬性的程式碼(如表 2-10 所示),最後 完成使用者介面(如圖 2-7 所示)。
表 2-9:AbsoluteLayout 中元件的屬性和說明。
屬性 說明
android:layout_x 設置元件的 X 座標 android:layout_y 設置元件的 Y 座標 android:layout_width 設置元件寬度 android:layout_height 設置元件高度
‧ 國
立 政 治 大 學
‧
N a tio na
l C h engchi U ni ve rs it y
19
表 2-10:Android 上實作 AbsoluteLayout 元件佈局。
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation=" vertical"
android:layout_weight="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:layout_weight="fill_paren"
android:layout_height="wrap_content" />
<Button
android:layout_x ="250px"
android:layout_y ="40px"
android:layout_weight="70px"
android:layout_height="wrap_content"
android:text="Button" />
</ AbsoluteLayout >
圖 2-7:AbsoluteLayout。