Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Fragment 入門教學

編輯:關於 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 的方式。執行結果如下:

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