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. 从零开始学ios开发(十二):Table Views(上)

    这次学习的控件非常重要且非常强大,是ios应用中使用率非常高的一个控件,可以说几乎每个app都会使用到它,它就是功能异常强大的Table Views.可以打开你的iphone中的phone.Messa ...

  2. Ubuntu 常用软件安装方法

    macubuntu 安裝方法: $wget https://github.com/downloads/ChinaLuo/Mac_Ubuntu/Mac_Ubuntu-12.04.tar.gz -O /t ...

  3. 修复jquery.treeview的增加子节点的方法的bug

    1.修复理由 在一个android项目中用到了treeview控件(本来自己通过android的原生api实现了一个http://www.cnblogs.com/Mr-Nobody/p/3527688 ...

  4. How to use Android Activity's finish(), onDestory() and System.exit(0) methods

    Activity.finish() Calling this method will let the system know that the programmer wants the current ...

  5. 1654 方程的解 - Wikioi

    题目描述 Description佳佳碰到了一个难题,请你来帮忙解决.对于不定方程a1+a2+… +ak-1 +ak=g(x),其中k≥2且k ∈ N*,x是正整数,g(x) =xx mod 1000( ...

  6. c++ g++3.4.5 g++4.8.2 由编译器引起的编译异常

    #include <memory> #include <string> #include <iostream> class Student { public: St ...

  7. shell dev null 是什么

    1:在不想把标准输出和标准出错信息输出到控制台,也不想重定向到文件时经常使用 2:不能忽略其读入功能.从/dev/null读入时都是0 3:系统的垃圾桶,类似于Windows的回收站,不同的是这个设备 ...

  8. 【锋利的JQuery-学习笔记】添加提示图片

    效果图: hot图片: (注意:这个图标本身就有抖动效果的,并不是由于标签<del>而具有抖动效果) 周期性抖动,起到提示的作用 html: <div class="jnC ...

  9. uva 10859

    刘书例题  树形dp #include <cstdio> #include <cstdlib> #include <cmath> #include <map& ...

  10. tomcat 解析(一)-文件解析

    做web项目,最常用的服务器就是Apache的tomcat.虽然一直在用tomcat,但都是仅限在使用的阶段,一直没有深入学习过.想深入学习tomcat,首推的肯定是官网:http://tomcat. ...