当咱们设计应用程序时,但愿可以尽最大限度的适配各类设备,包括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()方法有一个可选的字符串参数,用来指定事务的惟一名称,这个是非必须的。