標籤:
摘要: 當我們設計應用程式時,希望能夠盡最大限度的適配各種裝置,包括4寸屏、7寸屏、10寸屏等等,Android開發文檔給了我們參考,而且Google IO的app(二)也實現了這種思想,他們都是使用layout、layout-large裡 ...
當我們設計應用程式時,希望能夠盡最大限度的適配各種裝置,包括4寸屏、7寸屏、 10寸屏等等,Android開發文檔給了我們參考,而且Google IO的app(二)也實現了這種思想,他們都是使用layout、layout-large裡面不同的布局檔案實現的,下面是翻譯的developer.android.com一篇的文章,裡面的例子能詳細的看出layout、layout-large並使用Fragmen構建靈活的案頭。 當設計應用程式,你可以在不同的布局結構中重複使用Fragment,以支援眾多的螢幕尺寸,,在可用的螢幕空間上最佳化使用者體驗。例如在手持功能(如Nexus 4)上,一個屏顯示一個Fragment,在更大屏(如Nexus 7)上可以使用多個Fragment顯示資訊。如: 圖一 圖一中,在大屏中兩個Fragment顯示在一個屏中,但是手持功能中,需要兩屏顯示完,一屏只能顯示一個,他們之間需要相互引導。 FragmentManager類提供了一些方法,使您可以在Activity運行時添加,刪除和替換Fragment,以創造一個靈活、動態體驗。 添加Fragment到一個啟動並執行Activity 這裡不是如同 《 Android UI開發第十七篇——Android Fragment執行個體》中將標籤放到布局檔案。而是使用FragmentManager動態管理Fragment。FragmentManager建立一個FragmentTransaction,它提供了添加,刪除以及其他fragment事務的API。activity允許移除或者替換fragment需要有如下條件: 1、activity的onCreate()方法中添加初始化的fragment 2、fragment置放位置的布局中必須有一個視圖容器 程式例子中, res/layout/news_articles.xml檔案提供了視圖容器。 [html] view plaincopy
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/fragment_container"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
Activity中使用getSupportFragmentManager()擷取FragmentManager,之後調用beginTransaction去建立一個FragmentTransaction對象, 再調用add()方法即可添加一個fragment。 在activity中可以使用同一個FragmentTransaction對象去執行多個fragment事務,當做這樣操作時,必須調用commint()方法。 下面的代碼示範怎樣添加一個fragment到res/layout/news_articles.xml的layout: [java] view plaincopy
- 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);
-
- // Check that the activity is using the layout version with
- // the fragment_container FrameLayout
- if (findViewById(R.id.fragment_container) != null) {
-
- // However, if we‘re being restored from a previous state,
- // then we don‘t need to do anything and should return or else
- // we could end up with overlapping fragments.
- if (savedInstanceState != null) {
- return;
- }
-
- // Create an instance of ExampleFragment
- HeadlinesFragment firstFragment = new HeadlinesFragment();
-
- // In case this activity was started with special instructions from an Intent,
- // pass the Intent‘s extras to the fragment as arguments
- firstFragment.setArguments(getIntent().getExtras());
-
- // Add the fragment to the ‘fragment_container‘ FrameLayout
- getSupportFragmentManager().beginTransaction()
- .add(R.id.fragment_container, firstFragment).commit();
- }
- }
- }
這裡的fragment是在運行時添加到FrameLayout,而不是直接使用標籤定義在activity的布局中,activity可以移除它或者使用另外一個不同的fragment替換它。
替換Fragment 替換一個fragment的過程和添加Fragment的過程差不多,但是需要的是replace()方法,而不是add()方法。 需要注意的是,當執行fragment事務時,比如替換或者刪除一個fragment,如果想能回退到當前,你必須在你提交fragment事務之前調用 addToBackStack()方法。 當移除或替換fragment時將事務添加到堆棧中,被移除的Fragmeng沒有消亡,如果使用者返回,Fragment會重新啟動。如果沒有放入到堆棧中,當Fragment被替換或移除,Fragment會消亡。 下面是替換Fragment的例子: [java] view plaincopy
- // Create fragment and give it an argument specifying the article it should show
- ArticleFragment newFragment = new ArticleFragment();
- Bundle args = new Bundle();
- args.putInt(ArticleFragment.ARG_POSITION, position);
- newFragment.setArguments(args);
-
- FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
-
- // Replace whatever is in the fragment_container view with this fragment,
- // and add the transaction to the back stack so the user can navigate back
- transaction.replace(R.id.fragment_container, newFragment);
- transaction.addToBackStack(null);
-
- // Commit the transaction
- transaction.commit();
addToBackStack()方法有一個可選的字串參數,用來指定事務的唯一名稱,這個是非必須的。
參考:http://developer.android.com/training/basics/fragments/fragment-ui.html 圖二 Google IO APP /*** @author 張興業* http://blog.csdn.net/xyz_lmn* android開發進階群:241395671*/ |
Android UI開發第三十篇——使用Fragment構建靈活的案頭