本章节翻译自《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. HDU 1308 What Day Is It?(模拟,日期)

    解题报告:输入一个年月日,让你求出那一天是星期几,但是做这题之前必须先了解一点历史.首先在1582年之前,判断是否是闰年的标准是只要能被四整除就是闰年, 然后在1752年9月2号的后的11天被抹去了, ...

  2. 洛谷——P1722 矩阵 II

    P1722 矩阵 II 题目背景 usqwedf 改编系列题. 题目描述 如果你在百忙之中抽空看题,请自动跳到第六行. 众所周知,在中国古代算筹中,红为正,黑为负…… 给定一个1*(2n)的矩阵(us ...

  3. Jenkins集成SonarQube

    sonar安装配置 准备软件包 安装 mysql5.6+ 创建数据库sonar,字符集编码utf8 新建用户如heboan 因为sonarqube不能使用root用户运行 调整系统参数,/etc/se ...

  4. 表(Table)

    虽然我们已经将不同用途的物品保存在不同的仓库中了,但是在同一个仓库中数据的保存仍然存在问题.比如食品分为熟食.生肉.大米等,如果把他们随意的堆放在一起,就会造成我们无法很容易的对这些食品进行管理,当要 ...

  5. java中Frame Jpanel等中间显示

    几种是窗口中间显示的方法: 1.Frame int windowWidth = frame.getWidth(); //获得窗口宽 int windowHeight = frame.getHeight ...

  6. AtCoder - 2705 Yes or No

    Problem Statement You are participating in a quiz with N+M questions and Yes/No answers. It's known ...

  7. 求小于10000的素数的个数 Exercise06_10

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求小于10000的素数的个数 * */ public class Exercise06_10 { public static ...

  8. iOS viewDidLoad 什么时候调用

    - (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, ...

  9. ORA-12537:TNS:连接关闭 -------数据库最大连接数问题

    问题: 我自己用PLSQL登录实验,我也登陆不了,可是让同事实验,他一会能够登录,一会不能够登录.应用还是能够正常访问,只是PLSQL登录异常. 分析: 基于这种情况去百度,有的说是配置文件有问题,有 ...

  10. js基础的思维导图

    纯粹转载下,我可画不出这么好看的思维导图,这得感谢@ChokCoco javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript ...