http://www.lupaworld.com/article-222973-1.html

当我们设计应用程序时,希望能够尽最大限度的适配各种设备,包括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
plain
copy

  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:


[java] view
plain
copy

  1. import android.os.Bundle;
  2. import android.support.v4.app.FragmentActivity;
  3. public class MainActivity extends FragmentActivity {
  4. @Override
  5. public void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.news_articles);
  8. // Check that the activity is using the layout version with
  9. // the fragment_container FrameLayout
  10. if (findViewById(R.id.fragment_container) != null) {
  11. // However, if we're being restored from a previous state,
  12. // then we don't need to do anything and should return or else
  13. // we could end up with overlapping fragments.
  14. if (savedInstanceState != null) {
  15. return;
  16. }
  17. // Create an instance of ExampleFragment
  18. HeadlinesFragment firstFragment = new HeadlinesFragment();
  19. // In case this activity was started with special instructions from an Intent,
  20. // pass the Intent's extras to the fragment as arguments
  21. firstFragment.setArguments(getIntent().getExtras());
  22. // Add the fragment to the 'fragment_container' FrameLayout
  23. getSupportFragmentManager().beginTransaction()
  24. .add(R.id.fragment_container, firstFragment).commit();
  25. }
  26. }
  27. }

这里的fragment是在运行时添加到FrameLayout,而不是直接使用标签定义在activity的布局中,activity可以移除它或者使用另外一个不同的fragment替换它。

替换Fragment


替换一个fragment的过程和添加Fragment的过程差不多,但是需要的是replace()方法,而不是add()方法。
需要注意的是,当执行fragment事务时,比如替换或者删除一个fragment,如果想能回退到当前,你必须在你提交fragment事务之前调用 addToBackStack()方法。

当移除或替换fragment时将事务添加到堆栈中,被移除的Fragmeng没有消亡,如果用户返回,Fragment会重新启动。如果没有放入到堆栈中,当Fragment被替换或移除,Fragment会消亡。

下面是替换Fragment的例子:

[java] view
plain
copy

  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. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  7. // Replace whatever is in the fragment_container view with this fragment,
  8. // and add the transaction to the back stack so the user can navigate back
  9. transaction.replace(R.id.fragment_container, newFragment);
  10. transaction.addToBackStack(null);
  11. // Commit the transaction
  12. transaction.commit();
 

addToBackStack()方法有一个可选的字符串参数,用来指定事务的唯一名称,这个是非必须的。

参考:http://developer.android.com/training/basics/fragments/fragment-ui.html

图二 Google IO APP

Android UI开发第三十篇——使用Fragment构建灵活的桌面的更多相关文章

  1. Android UI开发第三十九篇——Tab界面实现汇总及比较

    Tab布局是iOS的经典布局,Android应用中也有大量应用,前面也写过Android中TAb的实现,<Android UI开发第十八篇——ActivityGroup实现tab功能>.这 ...

  2. Android UI开发第三十五篇——AppCompat实现Action Bar

    每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了.Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计. ...

  3. Android UI开发第三十一篇——Android的Holo Theme

    好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...

  4. 【转】Android UI开发第三十一篇——Android的Holo Theme

    好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...

  5. Android UI开发第三十二篇——Creating a Navigation Drawer

    Navigation Drawer是从屏幕的左侧滑出,显示应用导航的视图.官方是这样定义的: The navigation drawer is a panel that displays the ap ...

  6. Android UI开发第三十四篇——SlidingPaneLayout

    SlidingPaneLayout也是系统支持的高级控件,是Android团对在2013 google IO大会期间更新的Support库(Version 13)中新加入的重要的功能.它支持左右滑动菜 ...

  7. Android UI开发第三十六篇——使用Volley加载图片列表

    Android开发者可能会使用Universal Image Loader或者Square`s newer Picasso这些第三方的库去处理图片的加载,那么Volley是怎么加载图片列表的呢,这一篇 ...

  8. Android UI开发第三十三篇——Navigation Drawer For Android API 7

    Creating a Navigation Drawer中使用的Navigation Drawer的android:minSdkVersion="14",现在Android API ...

  9. Android UI开发第四十篇——ScrollTricks介绍

    ScrollTricks是一个开源控件,实现了两个简单功能: 1.Quick Return:向上滑动时,View也向上滑动并且消失,当向下滑动时,View马上出现.例如Google Now的搜索功能. ...

随机推荐

  1. 信息传递--NOIP2015 day1 T2--暴力

    这道题我用了判联通量加暴力,但联通量判炸了....然后从code[VS]上看到个不错的代码,就拿来了^_^... 基本思路是去掉环外的点,然后走每一个联通块. #include <iostrea ...

  2. 动态模板中 SWIPER 划不动问题

    原文: 地址:http://hao.jser.com/archive/8030/ 作者:segmentfault 问题: 动态循环生成swiper-slide类,在swiper-wrapper里生成6 ...

  3. cocos2d-x入门笔记(1)

    cocos2d-x的大致开发流程是,首先使用win32版进行代码编写并完成游戏,然后将代码迁移到对应的开发环境上进行交叉编译完成游戏打包,如iphone上是mac+xcode,android是ecli ...

  4. 用MSBuild和Jenkins搭建持续集成环境 - 转

    http://www.infoq.com/cn/articles/MSBuild-1 http://www.infoq.com/cn/articles/MSBuild-2 MSBuild是在.NET ...

  5. 3640: JC的小苹果 - BZOJ

    让我们继续JC和DZY的故事.“你是我的小丫小苹果,怎么爱你都不嫌多!”“点亮我生命的火,火火火火火!”话说JC历经艰辛来到了城市B,但是由于他的疏忽DZY偷走了他的小苹果!没有小苹果怎么听歌!他发现 ...

  6. 2109&2535: [Noi2010]Plane 航空管制 - BZOJ

    Description世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上, ...

  7. 1072: [SCOI2007]排列perm - BZOJ

    Description 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除(可以有前导0).例如123434有90种排列能被2整除,其中末位为2的有30种,末位为4的有60种.Input ...

  8. java.util.ConcurrentModificationException 解决办法(转)

    今天在项目的中有一个需求,需要在一个Set类型的集合中删除满足条件的对象,这时想当然地想到直接调用Set的remove(Object o)方法将指定的对象删除即可,测试代码:   public cla ...

  9. 【C# 反射泛型】

    C# 反射泛型 摘自:http://www.itwis.com/html/net/c/20110411/10175.html C#泛型反射和普通反射的区别,泛型反射和普通反射的区别就是泛型参数的处理上 ...

  10. iOS上的jQuery.on()冒泡事件绑定 以及 iOS绝对定位元素中的输入框

    上周遇到两个坑. 一是jQuery的on方法 事件冒泡,在iOS中有问题. $("body").on("click",".contentup" ...