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

Android Fragment入門:從零開始學習Fragment

編輯:關於 Android 程式設計

Android 3.0開始引入了一個新概念——Fragment。它有自己的版面配置檔,可以作為元件排布,也能相互組合去實現不同的版面顯示。使用Fragment可以重複利用程式碼,並滿足不同裝置尺寸的需求。Fragment不能單獨存在,只能存在於Activity中,而一個Activity可以擁有多個Fragment。很重要的一點是,Fragment可以和Activity中的其他元件一起使用,無需重寫所有Activity的介面。所以使用Fragment就能這樣完成「主介面—詳細介面」的App需求。

在手機上是這樣顯示的:

而在平板上是這樣的:

在一個小螢幕的裝置上,一個Activity通常占據整個螢幕,同時顯示各種UI視圖元件,Activity實際上就是視圖的容器。而當一個Activity被顯示在大螢幕裝置(例如平板電腦)上時,總會顯得有些不適應:螢幕太大,Activity中的所有UI元件要充滿整個螢幕,這樣一來視圖的層次結構就很複雜了。一個更好的辦法,是使用一種「輕量級」的Activity,每個「輕量級」Activity包含自己的視圖、互不干擾,在執行期間根據螢幕的方向和尺寸,一個Activity可以包含一個或多個「輕量級」Activity。在Android 3.0以上版本,這種「輕量級」Activity叫做Fragment。

怎麼建立一個Fragment

現在我們了解了Fragment的生命週期,接著就需要知道怎麼建立一個Fragment並綁定到Activity中。第一件要做的事,就是繼承android.app.Fragment來寫一個Fragment。假設我們的Fragment叫做Fragment1,建立與定義如下:

 public class Fragment1 extends Fragment { ... } 

就像我們上面說的,Fragment只能存在於Activity中,所以我們必須在某處定義它,有兩種方式:

- 直接在xml版面配置檔中定義;

- 在xml版面配置檔中定義一個佔位符,然後動態地在Activity中操作Fragment;

我們定義Fragment的方式會影響它的生命週期,因為在第一種情況下onInflate方法會被呼叫,而第二種情況下它的生命週期是從onAttach方法開始的。

如果我們在XML檔中定義Fragment,需要:

 <fragment android:id="@+id/f1" class="com.survivingwithandroid.fragment.Fragment1" android:layout_width="match_parent" android:layout_height="20dp"/> 

然而如果我們在XML中用佔位符,就需要再做一些工作。

版面配置框架和Fragment

如果我們在XML版面配置檔中定義Fragment,就不能自由、動態修改Fragment了。還有別的方法可以讓我們更靈活地操作:使用時需要在XML檔中定義:

 <FrameLayout android:id="@+id/fl1" android:layout_width="match_parent" android:layout_height="200dp"/> 

在Activity裡面還需要做一點工作,因為我們必須手動初始化Fragment,然後把它「插入」到FrameLayout中。

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Fragment2 f2 = new Fragment2(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.fl1, f2); ft.commit(); } 

例子

可以把Fragment想象成Activity的另外一種形式。你建立fragment去包含UI元件,就像建立Activity那樣,但是Fragment總是被嵌在Activity中。

下面透過一個例子來看一下流程:

1.建立一個名為Fragments的工程。

2.在res/layout文件夾下,新建一個叫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:id="@+id/lblFragment1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout> 

3.在res/layout文件夾下,新建一個叫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" /> <Button android:id="@+id/btnGetText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Get text in Fragment #1" android:textColor="#000000" /> </LinearLayout> 

4.main.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:orientation="horizontal" > <fragment android:id="@+id/fragment1" android:name="net.learn2develop.Fragments.Fragment1" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="net.learn2develop.Fragments.Fragment2" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout> 

5.新建兩個類:Fragment1.java和Fragment2.java。

6.Fragment1.java中的程式碼。

 package net.learn2develop.Fragments; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; 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) { Log.d("Fragment 1", "onCreateView"); // ---Inflate the layout for this fragment--- return inflater.inflate(R.layout.fragment1, container, false); } } 

7.Fragment2.java中的程式碼。

 package net.learn2develop.Fragments; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // ---Inflate the layout for this fragment--- return inflater.inflate(R.layout.fragment2, container, false); } } 

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