本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/

fragment的真正用处是在程序运行过程中动态地添加。

1. 新建工程。

2. res/layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal" >
  6. </LinearLayout>

3. res/layout/fragment1.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#00FF00"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:id="@+id/lblFragment1"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="This is fragment #1"
  12. android:textColor="#000000"
  13. android:textSize="25sp" />
  14. </LinearLayout>

4. res/layout/fragment2.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#FFFE00"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="This is fragment #2"
  11. android:textColor="#000000"
  12. android:textSize="25sp" />
  13. </LinearLayout>

5. Fragment1.java

  1. public class Fragment1 extends Fragment {
  2. @Override
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  4. Bundle savedInstanceState) {
  5. // ---Inflate the layout for this fragment---
  6. return inflater.inflate(R.layout.fragment1, container, false);
  7. }
  8. }

6. Fragment2.java

  1. public class Fragment2 extends Fragment {
  2. @Override
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  4. Bundle savedInstanceState) {
  5. // ---Inflate the layout for this fragment---
  6. return inflater.inflate(R.layout.fragment2, container, false);
  7. }
  8. }

7. FragmentsActivity.java

  1. public class FragmentsActivity extends Activity {
  2. /** Called when the activity is first created. */
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. FragmentManager fragmentManager = getFragmentManager();
  8. FragmentTransaction fragmentTransaction = fragmentManager
  9. .beginTransaction();
  10. // ---get the current display info---
  11. WindowManager wm = getWindowManager();
  12. Display d = wm.getDefaultDisplay();
  13. if (d.getWidth() > d.getHeight()) {
  14. // ---landscape mode---
  15. Fragment1 fragment1 = new Fragment1();
  16. // android.R.id.content refers to the content
  17. // view of the activity
  18. fragmentTransaction.replace(android.R.id.content, fragment1);
  19. } else {
  20. // ---portrait mode---
  21. Fragment2 fragment2 = new Fragment2();
  22. fragmentTransaction.replace(android.R.id.content, fragment2);
  23. }
  24. // ---add to the back stack---
  25. fragmentTransaction.addToBackStack(null);
  26. fragmentTransaction.commit();
  27. }
  28. }

8. 调试。

 

【Android 开发教程】动态添加Fragments的更多相关文章

  1. Android开发之动态添加控件

    动态添加TextView控件: 一:创建一个Android project项目 activity_main.xml文件: 1.用两个LinearLayout布局分别包裹一对TextView,EditT ...

  2. ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView

    原文地址: ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http:/ ...

  3. Android开发教程大全介绍

    Android是由谷歌在2007年推出的一个开放系统平台,主要针对移动设备市场,目前版本为Android 4.0.Android基于Linux,开发者可以使用Java或C/C++开发Android应用 ...

  4. ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map

    原文地址: ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NE ...

  5. ArcGIS Runtime for Android开发教程V2.0(2)开发环境配置

    原文地址: ArcGIS Runtime for Android开发教程V2.0(2)开发环境配置 - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.c ...

  6. ArcGIS Runtime for Android开发教程V2.0(1)基本概念

    原文地址: ArcGIS Runtime for Android开发教程V2.0(1)基本概念 - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.csd ...

  7. 收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。

    AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...

  8. Android开发教程AnimationDrawable逐帧播放动画

    下面我们一起来看篇Android开发AnimationDrawable控制逐帧播放动画实现过程,希望文章对各位朋友带不一些帮助. 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的 ...

  9. Android中如何动态添加碎片

    Android中的开发需要兼容手机和平板,两个方面.这就引入了碎片的概念.(注意:这里用的Fragment强烈建议使用support-v4库中的Fragment) 碎片:是一种可以嵌入在活动当中的UI ...

随机推荐

  1. 21、Django实战第21天:课程章节信息

    在课程详情页中,点击"开始学习",就进入到这课程章节信息,这里面包含了两个页面:"章节"和评论 1.把course-video.html(章节).course- ...

  2. Proxmox VE

    Proxmox虚拟化环境是基于QEMU/KVM和LXC的开源服务器虚拟化管理解决方案.我们可以使用集成的易于使用的WEB界面或通过CLI管理虚拟机,容器,高可用集群,存储和网络. Proxmox VE ...

  3. MongoError: topology was destroyed解决方法

    MongoError: topology was destroyed 分析得出,出现这个问题是因为,当mongodb尝试写入某个数据的时候,连接被中断了! 解决方法:检查代码中是否存在操作数据的过程中 ...

  4. Cent OS 运行 Cuberite

    Cuberite 是一个轻量级的Minecraft服务端,由C++编写,性能比Mojang等等用java写的高很多. 在腾讯云的最低端VPS上,用Spigot建服的话,从主世界传送到下界用时要五六秒的 ...

  5. Encode and Decode Strings -- LeetCode

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  6. [Atcoder SHPC2018] Tutorial

    Link: SHPC2018 传送门 C: 一道看上去有些吓人的题目,不过$1e9$规模下的$n^m$代表肯定是可以约分的 可以发现能提供贡献的数对只有$2*(n-d)$种,那么总贡献为$2*(n-d ...

  7. python3 开发面试题(生成列表)6.1

    话不多说直接上题: 生成如下列表: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12]] # 方式一 list1 ...

  8. Java如何实现系统监控、系统信息收集(转

    Java如何实现系统监控.系统信息收集.sigar开源API的学习 系统监控(1) Jar资源下载:http://download.csdn.net/detail/yixiaoping/4903853 ...

  9. Swift开发经验——外部参数名

    一.什么是外部参数名? 浅显地说,外部参数名就是在调用一个方法时要在方法的参数前面加上一个特定的名字,目的是便于阅读代码,提高维护效率.   二.在最新的Xcode中,外部参数名的性质与用法如下 性质 ...

  10. KVC与KVO的不同

    vc 就是一种通过字符串去间接操作对象属性的机制,  访问一个对象属性我们可以 person.age  也可以通过kvc的方式   [person valueForKey:@"age&quo ...