欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android App開發(fā)中創(chuàng)建Fragment組件的教程

 更新時(shí)間:2016年05月11日 10:12:45   作者:彭呈祥  
這篇文章主要介紹了Android App開發(fā)中創(chuàng)建Fragment的教程,Fragment是用以更靈活地構(gòu)建多屏幕界面的可UI組件,需要的朋友可以參考下

你可以認(rèn)為Fragment作為Activity的一個模塊部分,有它自己的生命周期,獲取它自己的事件,并且你可以在Activity運(yùn)行的時(shí)候添加或者移除它(有點(diǎn)像你可以在不同的Activity中重用的一個”子Activity“)。這節(jié)課程講述如何使用Support Library繼承Fragment類,所以你的應(yīng)用程序仍然是兼容運(yùn)行的系統(tǒng)版本低于Android1.6的設(shè)備。

注意:如果你決定你的應(yīng)用要求的最低的API級別是11或者更高,你不需要使用Support Library,反而能使用Frameword內(nèi)的Fragment和相關(guān)API。要注意,這節(jié)課程主要講使用Support Library的API,它使用特殊的包名,并且有些時(shí)候和包含在平臺中版本API的名稱略有不同。

在你開始這節(jié)課程之前,你必須配置你的Android項(xiàng)目使用Support Library。如果之前你沒有使用過Support Library,遵照Support Library Setup文檔,配置你的項(xiàng)目使用v4 Library。然而,你也能包含在你的Activity中Action Bar

創(chuàng)建Fragment
你可以把一個Fragment想象成一個Activity的模塊區(qū)域,它有自己的生命周期,接收它自己的輸入事件,并且你可以在Activity運(yùn)行時(shí)添加和刪除它(這有點(diǎn)像一個子Activity,你可以在不同的Activity中重用它們)。本節(jié)課向你介紹如何使用支持類庫來擴(kuò)展Fragment,以便讓你的應(yīng)用程序能夠在像Android1.6那樣的較舊版本上的兼容性。

注意:如果因?yàn)橐恍┰颍銢Q定你的應(yīng)用程序需要的API級別在11以上,那么你就不需要使用支持類庫,并且可以使用框架內(nèi)置的Fragment類和相關(guān)的API來代替。要注意的是本課的重點(diǎn)是使用支持類庫中的API,它使用一個特殊的包簽名,并且某些時(shí)候API的名稱會比包含在平臺內(nèi)的版本有稍微的不同。

創(chuàng)建Fragment類

要創(chuàng)建一個Fragment,就要繼承Fragment類,然后重寫關(guān)鍵的生命周期方法,把你的應(yīng)用程序邏輯插入其中,這跟Activity類類似。

創(chuàng)建Fragment時(shí)的一個不同點(diǎn)是,你必須使用onCreateView()回調(diào)來定義布局。實(shí)際上,為了獲得一個正在運(yùn)行的Fragment,這只是你所需要的唯一的回調(diào)方法。例如,下面是一個簡單的指定了自己布局的Fragment:

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.ViewGroup;

public class ArticleFragment extends Fragment {

  @Override

  public View onCreateView(LayoutInflater inflater, ViewGroup container,

    Bundle savedInstanceState) {

    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.article_view, container, false);

  }

}

就像一個Activity,F(xiàn)ragment應(yīng)該實(shí)現(xiàn)其他的生命周期回調(diào)方法,從而允許你管理它在Activity中的狀態(tài)(添加或刪除),以及Activity在它生命周期狀態(tài)間轉(zhuǎn)換時(shí)的狀態(tài)。例如,當(dāng)Activity的onPause()方法被調(diào)用時(shí),Activity中的任何Fragment也會接收到對onPause()方法的調(diào)用。

有關(guān)Fragment的生命周期和有效的回調(diào)方法,請看Fragments開發(fā)指南。

使用XML把一個Fragment添加到一個Activity中

Fragment是可復(fù)用的、模塊化的UI組件,每個Fragment類的實(shí)例都必須跟一個父類是FragmentActivity的Activity相關(guān)聯(lián)。通過在你的Activity布局XML文件內(nèi)定義每個Fragment可以完成這種關(guān)聯(lián)。

注意:FragmentActivity是一個支持類庫中提供的特殊的Activity,它用于處理系統(tǒng)版本是API Level 11以前的Fragment。如果你使用的系統(tǒng)版本最低是API Level 11或更高,那么就就可以使用常規(guī)的Activity。

當(dāng)屏幕被認(rèn)為足夠大時(shí),下例布局文件就會把兩個Fragment添加到一個Activity中(該文件被放在由large限定的目錄名中)。

res/layout-large/news_articles.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="horizontal"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  <fragment android:name="com.example.android.fragments.HeadlinesFragment"

       android:id="@+id/headlines_fragment"

       android:layout_weight="1"

       android:layout_width="0dp"

       android:layout_height="match_parent" />

  <fragment android:name="com.example.android.fragments.ArticleFragment"

       android:id="@+id/article_fragment"

       android:layout_weight="2"

       android:layout_width="0dp"

       android:layout_height="match_parent" />

</LinearLayout>

提示:關(guān)于給不同屏幕尺寸創(chuàng)建布局的信息,請看支持不同的屏幕尺寸。

以下是使用這個布局的Activity:

import android.os.Bundle;

import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

  @Override

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.news_articles);

  }

}

注意:當(dāng)你通過在布局XML文件中定義Fragment的方式把Fragment添加給Activity布局時(shí),你不能在運(yùn)行時(shí)刪除該Fragment。如果你打算在用戶交互期間切換Fragment,你就必須在Activity被首次啟動時(shí)把Fragment添加到Activity中。


相關(guān)文章

最新評論