<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                > 編寫:[fastcome1985](https://github.com/fastcome1985) - 原文:[http://developer.android.com/training/basics/fragments/creating.html](http://developer.android.com/training/basics/fragments/creating.html) - 我們可以把fragment想象成[activity](# "An activity represents a single screen with a user interface.")中一個模塊化的部分,它擁有自己的生命周期,接收自己的輸入事件,可以在acvitity運行過程中添加或者移除(有點像"子[activity](# "An activity represents a single screen with a user interface.")",可以在不同的[activity](# "An activity represents a single screen with a user interface.")里面重復使用)。這一課教我們將學習繼承[Support Library ](http://developer.android.com/tools/support-library/index.html)中的[Fragment](http://developer.android.com/reference/android/support/v4/app/Fragment.html),使應用在Android1.6這樣的低版本上仍能保持兼容。 > **Note:** 如果APP的最低API版本是11或以上,則不必使用Support Library,我們可以直接使用API框架中的[Fragment](http://developer.android.com/reference/android/app/Fragment.html),本課主要講解基于Support Library的API,Support Library有一個特殊的包名,有時與平臺版本的API名字存在略微不同。 - 在開始這節課前,必須先讓在項目中引用Support Library。如果沒有使用過Support Library,可以根據文檔 [Support Library Setup](http://developer.android.com/intl/zh-cn/tools/support-library/setup.html) 來設置項目使用Support Library。當然,也可以使用包含[action bar](http://developer.android.com/guide/topics/ui/actionbar.html)的 **v7 appcompat** library。v7 appcompat library 兼容Android2.1(API level 7),也包含了[Fragment](http://developer.android.com/reference/android/support/v4/app/Fragment.html) APIs。 ### 創建一個Fragment類 - 創建一個fragment,首先需要繼承[Fragment](http://developer.android.com/reference/android/support/v4/app/Fragment.html)類,然后在關鍵的生命周期方法中插入APP的邏輯,就像[activity](# "An activity represents a single screen with a user interface.")一樣。 - 其中一個區別是當創建[Fragment](http://developer.android.com/reference/android/support/v4/app/Fragment.html)的時,必須重寫[onCreateView()](http://developer.android.com/reference/android/support/v4/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle))回調方法來定義布局。事實上,這是使Fragment運行起來,唯一一個需要我們重寫的回調方法。比如,下面是一個自定義布局的示例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](# "An activity represents a single screen with a user interface.")一樣,當fragment從[activity](# "An activity represents a single screen with a user interface.")添加或者移除、當[activity](# "An activity represents a single screen with a user interface.")生命周期發生變化時,fragment通過生命周期回調函數管理其狀態。例如,當[activity](# "An activity represents a single screen with a user interface.")的onPause()被調用時,它里面的所有fragment的onPause()方法也會被觸發。 更多關于fragment的聲明周期和回調方法,詳見[Fragments](http://developer.android.com/guide/components/fragments.html) developer guide. ### 用XML將fragment添加到[activity](# "An activity represents a single screen with a user interface.") - fragments是可重用的,模塊化的UI組件,每個Fragment的實例都必須與一個[FragmentActivity](http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html)關聯。我們可以在[activity](# "An activity represents a single screen with a user interface.")的XML布局文件中定義每一個fragment來實現這種關聯。 > **Notes:**[FragmentActivity](http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html)是Support Library提供的一個特殊[activity](# "An activity represents a single screen with a user interface.") ,用于處理API11版本以下的fragment。如果我們APP中的最低版本大于等于11,則可以使用普通的[Activity](# "An activity represents a single screen with a user interface.")。 - 下面是一個XML布局的例子,當屏幕被認為是large(用目錄名稱中的`large`字符來區分)時,它在布局中增加了兩個fragment. 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> ~~~ > **Notes:**更多關于不同屏幕尺寸創建不同布局的信息,請閱讀[Supporting Different Screen Sizes](#) - 然后將這個布局文件用到[activity](# "An activity represents a single screen with a user interface.")中。 ~~~ 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); } } ~~~ - 如果用的是[ v7 appcompat library](http://developer.android.com/intl/zh-cn/tools/support-library/features.html#v7-appcompat),[activity](# "An activity represents a single screen with a user interface.")應該改為繼承[ActionBarActivity](http://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html),ActionBarActivity是FragmentActivity的一個子類(更多關于這方面的內容,請閱讀[Adding the Action Bar](http://developer.android.com/training/basics/actionbar/index.html))。 > **Note:**當通過XML布局文件的方式將Fragment添加進[activity](# "An activity represents a single screen with a user interface.")時,Fragment是不能被動態移除的。如果想要在用戶交互的時候把fragment切入與切出,必須在[activity](# "An activity represents a single screen with a user interface.")啟動后,再將fragment添加進[activity](# "An activity represents a single screen with a user interface.")。這部分內容將在下節課闡述。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看