参考自张泽华视频

Fragment是自Android3.0后引入的特性,主要用于在不同的屏幕尺寸中展现不同的内容。

Fragment必须被嵌入Activity中使用,总是作为Activity的组成部分。

简单示例:

一个Activity的界面由2个部分组成,每个部分分别是一个Fragment。

效果图如下:

1、创建第一个Fragment的界面文件。

Fragment的界面文件和一般的Activity布局文件一样,如。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#0000ff"
  6.     android:orientation="vertical" >
  7.     
  8.     <TextView 
  9.         android:layout_width="match_parent"
  10.         android:layout_height="match_parent"
  11.         android:text="@string/fragment1"
  12.         />
  13.  
  14. </LinearLayout>

2、创建第一个Fragment的类文件。

  1. package com.ljh.fragmentdemo;
  2.  
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8.  
  9. //继续Fragment类。
  10. public class Fragment1 extends Fragment {
  11.  
  12. @Override
  13. //在Fragment被加载时调用
  14. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  15. Bundle savedInstanceState) {
  16. //加载某个布局文件。
  17. return inflater.inflate(R.layout.fragment1, null);
  18. }
  19. }

3、创建第二个Fragment的界面文件。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#00ff00"
  6. android:orientation="vertical" >
  7.  
  8. <TextView
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:text="@string/fragment2"/>
  12.  
  13. </LinearLayout>

4、创建第二个Fragment的类文件。

  1. package com.ljh.fragmentdemo;
  2.  
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8.  
  9. //继续Fragment类。
  10. public class Fragment2 extends Fragment {
  11.  
  12. @Override
  13. //在Fragment被加载时调用
  14. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  15. Bundle savedInstanceState) {
  16. //加载某个布局文件。
  17. return inflater.inflate(R.layout.fragment2, null);
  18. }
  19.  
  20. }

5、创建主界面文件。

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >
  10.  
  11.     <fragment
  12.         android:id="@+id/fragment1"
  13.         android:name="com.ljh.fragmentdemo.Fragment1"
  14.         android:layout_width="0dp"
  15.         android:layout_height="match_parent"
  16.         android:layout_weight="1" />
  17.  
  18.     <fragment
  19.         android:id="@+id/fragment2"
  20.         android:name="com.ljh.fragmentdemo.Fragment2"
  21.         android:layout_width="0dp"
  22.         android:layout_height="match_parent"
  23.         android:layout_weight="1" />
  24.  
  25. </LinearLayout>

6、创建主类。

本步骤的关键是用Fragment对象取代布局文件中的内容。

  1. package com.ljh.fragmentdemo;
  2.  
  3. import com.ljh.fragmentdemo.R;
  4.  
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.app.FragmentManager;
  8. import android.app.FragmentTransaction;
  9.  
  10. public class MainActivity extends Activity {
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16.  
  17. //获得FragmentManager,然后获取FragmentTransaction。
  18. FragmentManager fm = getFragmentManager();
  19. FragmentTransaction transaction = fm.beginTransaction();
  20. //用Fragment动态代替布局文件中的内容
  21. transaction.replace(R.id.fragment1, new Fragment1());
  22. transaction.replace(R.id.fragment2, new Fragment2());
  23. //提交事务
  24. transaction.commit();
  25. }
  26.  
  27. }

注:

1、对两个Fragment的分别进行编辑,如

  1. @Override
  2. //在Fragment被加载时调用
  3. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  4. Bundle savedInstanceState) {
  5. //加载某个布局文件。
  6. View root = inflater.inflate(R.layout.fragment1, null);
  7.  
  8. TextView tvContent = (TextView) root.findViewById(R.id.tv_content);
  9. tvContent.setText("hello");
  10.  
  11. return root;
  12. }

2、其它内容见后面博客:

(1)Android2.3及以前对Fragment的支持。

(2)使用Fragment使不同的尺寸的屏幕展现不同内容。

(3)Activity与Fragment之间的通信。

版权声明:本文为博主原创文章,未经博主允许不得转载。

Fragment之一:Fragment入门 分类: H1_ANDROID 2013-11-15 18:16 2799人阅读 评论(2) 收藏的更多相关文章

  1. C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏

    1.     概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...

  2. 【solr专题之一】Solr快速入门 分类: H4_SOLR/LUCENCE 2014-07-02 14:59 2403人阅读 评论(0) 收藏

    一.Solr学习相关资料 1.官方材料 (1)快速入门:http://lucene.apache.org/solr/4_9_0/tutorial.html,以自带的example项目快速介绍发Solr ...

  3. The Happy Worm 分类: POJ 排序 2015-08-03 18:57 5人阅读 评论(0) 收藏

    The Happy Worm Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 4698 Accepted: 1047 Descr ...

  4. Rebuild my Ubuntu 分类: ubuntu shell 2014-11-08 18:23 193人阅读 评论(0) 收藏

    全盘格式化,重装了Ubuntu和Windows,记录一下重新配置Ubuntu过程. //build-essential sudo apt-get install build-essential sud ...

  5. 灰度世界算法(Gray World Algorithm) 分类: 图像处理 Matlab 2014-12-07 18:40 874人阅读 评论(0) 收藏

    人的视觉系统具有颜色恒常性,能从变化的光照环境和成像条件下获取物体表面颜色的不变特性,但成像设备不具有这样的调节功能, 不同的光照环境会导致采集的图像颜色与真实颜色存在一定程度的偏差,需要选择合适的颜 ...

  6. Pots 分类: 搜索 POJ 2015-08-09 18:38 3人阅读 评论(0) 收藏

    Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11885 Accepted: 5025 Special Judge D ...

  7. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. Ubuntu vim+ ctags(包含系统函数) + taglist 配置 分类: vim ubuntu 2015-06-09 18:19 195人阅读 评论(0) 收藏

    阅读大型代码,我们经常需要打开很多的代码文件,搜索各种定义.windows下用惯了ide的朋友,转战Linux的时候可能会觉得很难受,找不到合适的阅读工具.其实万能的vim就可以实现.下面介绍一下vi ...

  9. HTTP 错误 500.19- Internal Server Error 错误解决方法 分类: Windows服务器配置 2015-01-08 20:16 131人阅读 评论(0) 收藏

    1.第一种情况如下: 解决方法如下: 经过检查发现是由于先安装Framework组件,后安装iis的缘故,只需重新注册下Framework就可以了,具体步骤如下 1 打开运行,输入cmd进入到命令提示 ...

随机推荐

  1. Codeforces Round #316 (Div. 2) A B C

    A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  2. GridView-属性大全

    这是个网格控件 他的实现也是通过adapter来实现的,感觉跟listview在使用上并没有多大的区别 常见属性如下 1.android:numColumns=”auto_fit” //GridVie ...

  3. 17.Node.js 回调函数--异步编程

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程 ...

  4. Flume的Collector

    Collector的作用是将多个Agent的数据汇总后,加载到Storage中.它的source和sink与agent类似. 数据源(source),如: collectorSource[(port) ...

  5. 基于mybatis的BaseDao及BaseService深度结合(转)

    原文地址:http://zhaoshijie.iteye.com/blog/2003209 关键字:Mybatis通用DAO设计封装(mybatis) 说明: mybatis默认分页机制为逻辑分页,所 ...

  6. Android时间戳与字符串相互转换

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  7. js进阶 14-8 表单序列化函数serializeArray()和serialize()的区别是什么

    js进阶 14-8 表单序列化函数serializeArray()和serialize()的区别是什么 一.总结 一句话总结:两者都是对表单进行序列化,serializeArray()返回的是json ...

  8. JQuery滑动到指定位置

    $('html, body').animate({ scrollTop: next_tip.offset().top + "px"},500);

  9. h5背景

    1.背景属性复习: background-image background-color background-repeat background-position background-attachm ...

  10. Android开发人员应该知道的Kotlin

    本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2016/01/kotlin-android Android开发人员在语言限制方面面临着 ...