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

Android 入門:Fragment 的靜態與動態嵌入

編輯:關於 Android 程式設計

可以把 Fragment 理解成一個「小型 Activity」——幾乎 Activity 能做的事它都能做,差別在於 Fragment 可以嵌入 Activity。一個 Activity 能裝多個 Fragment,還能在運行時依需求切換,這正是為了適配不同螢幕尺寸而生的設計。

嵌入 Fragment 有兩條路:靜態用 XML 宣告,動態則在 Java 程式碼中完成。

步驟一:建立 Fragment1

package bill.su.fragment;

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, container, false);
    }
}

對應的版面 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:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#00FF00" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/fragment_one"
        android:textSize="25sp"
        android:textColor="#FF0000" />

</LinearLayout>

步驟二:建立 Fragment2

package bill.su.fragment;

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, container, false);
    }
}

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:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FFFE00" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/fragment_two"
        android:textColor="#000000"
        android:textSize="25sp" />

</LinearLayout>

靜態嵌入:直接在 XML 裡放 fragment 標籤

<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"
    tools:context="bill.su.fragment.MainActivity"
    android:orientation="horizontal" >

    <fragment
        android:name="bill.su.fragment.Fragment1"
        android:id="@+id/fragment1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:name="bill.su.fragment.Fragment2"
        android:id="@+id/fragment2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

用 layout_weight 平均分配寬度,就能得到左右並排的兩欄效果:

動態嵌入:靠 FragmentManager 與 FragmentTransaction

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FragmentManager fragManager = getFragmentManager();
    FragmentTransaction fragTrans = fragManager.beginTransaction();

    // 取得螢幕顯示資訊,判斷橫屏或豎屏
    WindowManager wm = getWindowManager();
    Display d = wm.getDefaultDisplay();

    if (d.getWidth() < d.getHeight()) {
        Fragment1 frag1 = new Fragment1();
        fragTrans.replace(android.R.id.content, frag1);
    } else {
        Fragment2 frag2 = new Fragment2();
        fragTrans.replace(android.R.id.content, frag2);
    }

    fragTrans.commit();
}

幾個關鍵知識點

動態載入靠的是 FragmentManager 與 FragmentTransaction 這兩個類別搭配使用。

範例中的 WindowManager 用來取得螢幕長寬,判斷目前是橫屏還是豎屏,再據此載入不同的 Fragment,呈現不同版面。

最後那句 fragTrans.commit() 不能省——少了它,Fragment 不會真正被載入。另外要注意 commit 是非同步的,如果你需要在提交後立刻操作 Fragment,應該改用 commitNow() 或透過 executePendingTransactions 處理。

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