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

Android 學習筆記(十三):碎片 Fragment 入門(一)

編輯:關於 Android 程式設計

碎片(Fragment)

碎片可以看作另一種形式的活動,我們可以建立碎片來容納檢視。碎片總是嵌入在活動之中,一般有兩種常見形式:

1、碎片 A 與碎片 B 分別處於不同的活動中,當選擇碎片 A 中的某一項時,觸發碎片 B 啟動;

2、碎片 A 與碎片 B 處於同一個活動中,共享同一活動,以創造更佳的用戶體驗。

點此下載完整源碼(程式碼適用於本文所講內容)。

下面跟著步驟動手實作:

1、建立一個名為「Fragments」的專案,在 res/layout 資料夾下分別新建 fragment1.xml、fragment2.xml;在當前套件名稱下分別新建 Fragment1.java、Fragment2.java。

fragment1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00FF00"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #1"
        android:textColor="#000000"
        android:textSize="25sp" />
</LinearLayout>

fragment2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFE00"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #2"
        android:textColor="#000000"
        android:textSize="25sp" />
</LinearLayout>

Fragment1.java:

package net.zenail.fragments;
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 {
    // 繪製碎片 UI:使用 LayoutInflauter 物件來增大指定 XML 檔案中的 UI。
    // container 參數引用父 ViewGroup,準備用於嵌入碎片的活動。
    // savedInstanceState 參數允許將碎片還原到前一次保存的狀態。
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }
}

Fragment2.java:

package net.zenail.fragments;
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);
    }
}

2、在 main.xml 檔案中加入兩個碎片:

<fragment
    android:id="@+id/fragment1"
    android:name="net.zenail.fragments.Fragment1"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1"
    tools:layout="@layout/fragment1" />
<fragment
    android:id="@+id/fragment2"
    android:name="net.zenail.fragments.Fragment2"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1"
    tools:layout="@layout/fragment2" />

3、運行,效果如下:

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