編輯:進階開發
Fragment(片段)代表應用程式中的一塊使用者介面或行為,目的是讓 Activity 的設計更模組化、可以拆解重組。你可以把它理解成一種「子 Activity」。關於 Fragment,有幾個重點要先掌握:
建立 Fragment 的方式是繼承 Fragment 類別;若要把它宣告在版面中,則於 Activity 的配置檔裡使用 <fragment> 元素插入。
在 Fragment 出現之前,Android 有個先天限制:同一時間點螢幕上只能顯示單一 Activity,因此無法把畫面切開、各自獨立運作。Fragment 的導入打破了這個限制——畫面上仍只有一個 Activity,但它可以容納多個 Fragment,每個 Fragment 都有自己的版面、邏輯與完整生命週期。
下面是一個很典型的應用情境:兩個 UI 模組以 Fragment 形式定義,在平板裝置上組合成單一 Activity,而在手機上則拆成兩個畫面。
在平板尺寸的裝置上,Activity A 同時嵌入這兩個 Fragment;但在手機螢幕上空間不足,於是 Activity A 只顯示文章清單那個 Fragment,使用者點選某篇文章後,才啟動 Activity B 來顯示內容 Fragment。
Fragment 的生命週期與 Activity 相當類似,可以拆成幾個階段來看。
階段一:建立——Fragment 被建立時依序經過:
階段二:可見——Fragment 出現在使用者面前:
階段三:退到背景——Fragment 進入背景模式:
階段四:銷毀——Fragment 被破壞:
實作流程可以濃縮成四步:
以下三個方法是最常被覆寫的:
接下來建立一個名為 MyFragments 的範例,包含兩個 Fragment,並依裝置方向自動切換:橫向時用一個,縱向時用另一個。
| 步驟 | 說明 |
|---|---|
| 1 | 用 Eclipse IDE 建立 Android 專案 MyFragments,套件 com.example.myfragments,使用空白 Activity。 |
| 2 | 修改 MainActivity.java,加入方向判斷與 Fragment 切換邏輯。 |
| 3 | 在 com.example.myfragments 下建立 LM_Fragment.java 與 PM_Fragment.java。 |
| 4 | 建立 res/layout/lm_fragment.xml 等版面檔。 |
| 5 | 修改 res/layout/activity_main.xml,放入 Fragment。 |
| 6 | 在 res/values/strings.xml 定義所需常數。 |
| 7 | 執行應用程式並驗證結果。 |
以下是 MainActivity.java 的內容:
package com.example.myfragments; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.res.Configuration; import android.view.WindowManager; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Configuration config = getResources().getConfiguration(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); /** * Check the device orientation and act accordingly */ if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { /** * Landscape mode of the device */ LM_Fragment ls_fragment = new LM_Fragment(); fragmentTransaction.replace(android.R.id.content, ls_fragment); }else{ /** * Portrait mode of the device */ PM_Fragment pm_fragment = new PM_Fragment(); fragmentTransaction.replace(android.R.id.content, pm_fragment); } fragmentTransaction.commit(); } }
接著是兩個 Fragment 類別。LM_Fragment.java:
package com.example.myfragments; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class LM_Fragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * Inflate the layout for this fragment */ return inflater.inflate( R.layout.lm_fragment, container, false); } }
PM_Fragment.java:
package com.example.myfragments; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class PM_Fragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * Inflate the layout for this fragment */ return inflater.inflate( R.layout.pm_fragment, container, false); } }
兩個版面檔放在 res/layout 下。lm_fragment.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#7bae16"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/landscape_message" android:textColor="#000000" android:textSize="20px" /> <!-- More GUI components go here --> </LinearLayout>
pm_fragment.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#666666"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/portrait_message" android:textColor="#000000" android:textSize="20px" /> <!-- More GUI components go here --> </LinearLayout>
activity_main.xml 則以 <fragment> 元素放入片段:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <fragment android:name="com.example.fragments" android:id="@+id/lm_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.fragments" android:id="@+id/pm_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
最後確認 res/values/strings.xml 的內容:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MyFragments</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="landscape_message">This is Landscape mode fragment </string> <string name="portrait_message">This is Portrait mode fragment </string> </resources>
現在執行剛建立的 MyFragments。請確認 AVD 與環境都已設定好,開啟專案 Activity 檔並點擊工具列上的「Run」
圖示。Eclipse 會把 App 裝上 AVD 並啟動,模擬器視窗如下,可能需要按一下「MENU」鈕(首次啟動會稍慢,請依電腦效能耐心等候):
要旋轉模擬器畫面,可以:
切換方向之後,畫面就會換成另一個 Fragment 呈現的介面。以下就是橫向模式的結果:
如此一來,同一個 Activity 就能依情境套用不同的 GUI,而差異完全封裝在各自的 Fragment 裡。只要更換 Fragment 的實作,就能組合出各式各樣的畫面。
Android 1.5 Cupcake 正式登場:G1 首波升級即刻開跑
2009 年 4 月 27 日,Google 正式對外發表 Android 1.5 SDK,代號「Cupcake」的這一代版本,也成為 Android 從雛形走向實用的重
NDK 入門專案實戰(一)
目標:利用 NDK 生成 SO 函式庫,透過 SO 函式庫進行 JNI 呼叫,在 Android sdcard 建立檔案並寫入資料。 工具:NDK 1.5 R1、Andr
Android 原始碼結構(6):把對應版本的源碼掛進 SDK
下載 frameworks 之後,會把 android.git.kernel.org 上 base 資料夾裡的所有檔案同步到本地的 frameworks 目錄中。 接著在
谷歌如何駕馭 Android 的雙面效應
Android 3.0 SDK 正式發布,Android 前幾個版本也已證明自己夠成功。但在全球成功背後,手機製造商對它的情感其實相當複雜:愛恨交織。 對 Dell、Ac