當咱們設計應用程序時,但願可以盡最大限度的適配各類設備,包括4寸屏、7寸屏、10寸屏等等,Android開發文檔給了咱們參考,並且Google IO的app(如圖二)也實現了這種思想,他們都是使用layout、layout-large裏面不一樣的佈局文件實現的。當設計應用程序,你能夠在不一樣的佈局結構中重複使用Fragment,以支持衆多的屏幕尺寸,,在可用的屏幕空間上優化用戶體驗。例如在手持設備(如Nexus 4)上,一個屏顯示一個Fragment,在更大屏(如Nexus 7)上可使用多個Fragment顯示信息。以下圖:android
圖一中,在大屏中兩個Fragment顯示在一個屏中,可是手持設備中,須要兩屏顯示完,一屏只能顯示一個,他們之間須要相互引導。app
FragmentManager類提供了一些方法,使您能夠在Activity運行時添加,刪除和替換Fragment,以創造一個靈活、動態的體驗。ide
這裏使用FragmentManager動態的管理Fragment。FragmentManager建立一個FragmentTransaction,它提供了添加,刪除以及其餘fragment事務的API。activity容許移除或者替換fragment須要有以下條件:佈局
程序例子中, res/layout/news_articles.xml文件提供了視圖容器。優化
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/fragment_container" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" />
Activity中使用getSupportFragmentManager()獲取FragmentManager,以後調用beginTransaction去建立一個FragmentTransaction對象, 再調用add()方法便可添加一個fragment。 在activity中可使用同一個FragmentTransaction對象去執行多個fragment事務,當作這樣操做時,必須調用commint()方法。 下面的代碼演示怎樣添加一個fragment到res/layout/news_articles.xml的layout:this
1 import android.os.Bundle; 2 import android.support.v4.app.FragmentActivity; 3 4 public class MainActivity extends FragmentActivity { 5 @Override 6 public void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.news_articles); 9 10 // Check that the activity is using the layout version with 11 // the fragment_container FrameLayout 12 if (findViewById(R.id.fragment_container) != null) { 13 14 // However, if we're being restored from a previous state, 15 // then we don't need to do anything and should return or else 16 // we could end up with overlapping fragments. 17 if (savedInstanceState != null) { 18 return; 19 } 20 21 // Create an instance of ExampleFragment 22 HeadlinesFragment firstFragment = new HeadlinesFragment(); 23 24 // In case this activity was started with special instructions from an Intent, 25 // pass the Intent's extras to the fragment as arguments 26 firstFragment.setArguments(getIntent().getExtras()); 27 28 // Add the fragment to the 'fragment_container' FrameLayout 29 getSupportFragmentManager().beginTransaction() 30 .add(R.id.fragment_container, firstFragment).commit(); 31 } 32 } 33 }
這裏的fragment是在運行時添加到FrameLayout,而不是直接使用<fragment>標籤訂義在activity的佈局中,activity能夠移除它或者使用另一個不一樣的fragment替換它。spa
替換一個fragment的過程和添加Fragment的過程差很少,可是須要的是replace()方法,而不是add()方法。 須要注意的是,當執行fragment事務時,好比替換或者刪除一個fragment,若是想能回退到當前,你必須在你提交fragment事務以前調用 addToBackStack()方法。 設計
當移除或替換fragment時將事務添加到堆棧中,被移除的Fragmeng沒有消亡,若是用戶返回,Fragment會從新啓動。若是沒有放入到堆棧中,當Fragment被替換或移除,Fragment會消亡。rest
下面是替換Fragment的例子:code
1 // Create fragment and give it an argument specifying the article it should show 2 ArticleFragment newFragment = new ArticleFragment(); 3 Bundle args = new Bundle(); 4 args.putInt(ArticleFragment.ARG_POSITION, position); 5 newFragment.setArguments(args); 6 7 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 8 9 // Replace whatever is in the fragment_container view with this fragment, 10 // and add the transaction to the back stack so the user can navigate back 11 transaction.replace(R.id.fragment_container, newFragment); 12 transaction.addToBackStack(null); 13 14 // Commit the transaction 15 transaction.commit();
addToBackStack()方法有一個可選的字符串參數,用來指定事務的惟一名稱,這個是非必須的。