編輯:關於 Android 程式設計
Fragment 可以理解為 Activity 介面中的一個片段,或是一種行為單元。把多個 Fragment 組合成一個 Activity,就能拼出多畫面的介面;而同一個 Fragment 也能在不同 Activity 之間複用。本質上,它是 Activity 的一段模組化程式碼,擁有自己的生命週期,能接收自己的事件,並在 Activity 執行過程中動態加入或移除。
Fragment 無法獨立存在,必須依附在 Activity 之上,其生命週期直接受宿主 Activity 影響:Activity 暫停時,它擁有的所有 Fragment 一起暫停;Activity 銷毀時,Fragment 也隨之銷毀。反過來說,當 Activity 處於執行狀態(onResume 之後、onPause 之前),你可以針對每個 Fragment 單獨操作,例如加入或刪除。
這些針對 Fragment 的操作被稱為「事務」(Transaction),由 Activity 管理並可放入返回堆疊。堆疊中的每一筆就是一 Fragment 的一次事務,有了它,就能反向回退這些操作,從而在 Fragment 層級支援返回鍵的導航行為。
把 Fragment 加入 Activity 時,它必須置於某個 ViewGroup 容器中並定義自己的介面。宣告方式有兩種:在 layout.xml 中以 <fragment> 標籤靜態聲明,或在程式碼中建立後加入 ViewGroup。值得一提的是,Fragment 也可以完全不顯示介面,隱身在背景為 Activity 工作。
先在專案的佈局檔中放入兩個 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" android:orientation="horizontal" tools:context=".MainActivity" > <fragment android:id="@+id/fragment1" android:name="com.wuyudong.fragment.Fragment1" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" > </fragment> <fragment android:id="@+id/fragment2" android:name="com.wuyudong.fragment.Fragment2" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" > </fragment> </LinearLayout>
接著在 layout 資料夾下新增各自的佈局檔。
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:background="#0000ff" android:orientation="vertical" > </LinearLayout>
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:background="#ff00" android:orientation="vertical" > </LinearLayout>
然後在原始碼資料夾下建立對應的兩個 Java 檔。
Fragment1.java
public class Fragment1 extends Fragment { /** * 當fragment被創建的時候,調用的方法,返回當前fragment顯示的內容 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, null); } }
Fragment2.java
public class Fragment2 extends Fragment { /** * 當fragment被創建的時候,調用的方法,返回當前fragment顯示的內容 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, null); } }
執行專案,可以看到左右並排顯示兩個不同顏色的區塊:

接下來改成執行期決定要放哪一個 Fragment。做法很簡單:把 activity_main.xml 裡的兩個 <fragment> 標籤刪掉,其餘不動,然後在 MainActivity 中補上這段程式碼。
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1、判斷當前手機的朝向 int width = getWindowManager().getDefaultDisplay().getWidth(); int height = getWindowManager().getDefaultDisplay().getHeight(); Fragment1 fragment1 = new Fragment1(); Fragment2 fragment2 = new Fragment2(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); if (width > height) { // 水平方向 ft.replace(android.R.id.content, fragment1); } else { ft.replace(android.R.id.content, fragment2); } ft.commit(); } }
這段程式碼的效果是:依手機當前朝向不同,顯示不同的 Fragment。
如果希望使用者能按返回鍵回退這次切換,只要在 commit 之前多呼叫一句 addToBackStack(null) 即可。
以上就是 Fragment 動態創建的完整示範,希望能幫助到你。
RxJava2 核心變化全解析
最近剛把 RxJava1.x 的源碼讀完,正好 RxJava2.x 也放出了 RC 版本。趁著正式版發布前的空檔,本文就來對 RxJava2.x 做一次入門級的剖析。
Android 庫項目使用方法圖文解析
在 Android 開發中,程式碼與資源的重用非常重要。若可重用的內容只有 Java 程式碼,通常可以打包成 jar 檔供其他專案使用;但若包含圖片、版面配置、字串資源或
Android 源碼剖析:setContentView 與 LayoutInflater 的加載解析機制
一、為什麼要談這個話題 有兩個原因。第一,我們平常寫界面幾乎都是在 onCreate 裡直接 setContentView 就完事,很少有人去關注 XML 控件到底是怎麼
SpannableString 實戰:讓 TextView 中的部分文字可點擊
Android 裡各種文本樣式(前景色、背景色、字號、點擊、鏈接等)都是通過 Span 封裝的,而 Span 需要裝進 SpannableString,再把 Spanna