編輯:關於 Android 程式設計
Fragment 是 Android 3.0 才引入的概念。Google 推出 Fragment 的主要目的,是用於大螢幕裝置(例如平板電腦),以支援更動態、更靈活的 UI 設計。平板螢幕遠比手機大得多,有更多空間放置 UI 元件,元件之間也會產生更多互動。Fragment 正好允許這樣的設計,無須你親自管理 view hierarchy 的複雜變化。
透過將 Activity 的版面配置分散到各個 Fragment 中,你可以在執行期修改 Activity 的外觀。可以把 Fragment 視為 Activity 畫面上的一部分。先來看下面兩張示意圖:


第一張圖中,點擊左側的項目會跳轉到另一個 Activity 顯示內容;而第二張圖中,點擊左側項目可直接在右側以 Fragment 顯示,無須啟動新的 Activity。由於 Activity 是 Android 的四大元件,佔用的記憶體比 Fragment 大得多,這正是在大螢幕上推薦使用 Fragment 的原因。
以下示範靜態建立 Fragment 的做法。
activity_main.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<fragment
android:name="com.example.fragment1.Fragment1"
android:id="@+id/fragment1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:name="com.example.fragment1.Fragment2"
android:id="@+id/fragment2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
版面檔中出現了一個 fragment 節點,而我們過去使用的 view 都以大寫字母開頭(如 TextView),因此 fragment 並不是一個 view 物件,而是一種類型。android:name 指的是 Fragment 類別的完整類名,所以 Fragment1 必須繼承 Fragment。若使用 android.app.Fragment 套件,指定的最小版本必須為 11(android:minSdkVersion="11"),小於 11 程式便會報錯,因為系統的 Fragment 是 3.0 才出現的。
Fragment1.java
package com.example.fragment1;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, null);
}
}
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff0000"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第一個 fragment"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Fragment2.java
package com.example.fragment1;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, null);
}
}
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#0000ff"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第二個 fragment"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity.java
package com.example.fragment1;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
可以看到 MainActivity 中並未撰寫任何程式碼,這便是靜態建立 Fragment 的方式。執行結果如下:

Android Fragment 使用大全:從建立、動態切換到互相溝通
Fragment 代表 Activity 中的一段行為或一部分使用者介面。它的核心價值有兩個:一是可以在單一 Activity 中組合多個 Fragment 構成多面板版
Android 自訂 ViewGroup 實作支付寶咻一咻雷達脈衝動畫
支付寶的咻一咻活動中,有個很典型的雷達擴散動畫:多個圓圈依序放大,同時透明度逐漸降低,形成連續掃描的效果。這篇文章會用自訂 ViewGroup 搭配屬性動畫,實作類似視覺
Android Activity 生命週期詳解:回呼時機與實測驗證
子曰:溫故而知新,可以為師矣。《論語》讀技術也一樣。想靠一遍就把官方文件或經典書籍讀通,基本上不太可能,所以每隔一段時間回頭重讀,才能慢慢體會作者真正想傳達的意思。最近把
動手做一個 ListView 下拉刷新:自訂 View 與狀態機的完整流程
下拉刷新幾乎是每個資訊類 App 的標配手勢。它看起來簡單,實作起來卻要同時處理觸控事件、狀態切換、視圖偏移與動畫。本文以一個自訂 MyListView 為例,把整個流程