一直知道Fragment很强大,但是一直都没有去学习,现在有些空闲的时间,所以就去学习了一下Fragment的简单入门。我也会把自己的学习过程写下来,如果有什么不足的地方希望大牛指正,共同进步!

  一、Fragment简介

    1.Fragment作为Activity界面的一部分组成出现;

    2.可以在一个Activity中同时出现多个Fragment,并且,一个Fragment亦可在多个Activity中使用;

    3.在Activity运行过程中,可以添加、移除或者替换Fragment(add()、remove()、replace());

    4.Fragment可以响应自己的输入事件,并且有自己的生命周期,当然,它们的生命周期直接被其所属的activity的生命周期影响。

    那我们为什么要用Fragment呢?主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计。平板电脑的屏幕要比手机的大得多,有更多的空间来放更多的UI组件,并且这些组件之间会产生更多的交互。我们可以把Fragment认为是“小的Activity”,Fragment更加简洁。

  二、Fragment的简单使用

    那我们就简单的显示2个Fragment为例来讲解一下。

    

    1.在XML中添加Fragment:

      新建Fragment1、Fragment2(注意:这里可能有2个包可以选择导入android.app.Fragment或android.support.v4.app.Fragment都是可以的,我这里选择使用了前者,但是两者使用时有区别的,在结尾中我会讲到):

      Fragment1代码:

 package com.example.fragment;

 import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.example.fragmentdemo.R; public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("TAG", "in");
return inflater.inflate(R.layout.fragment1, container, false);
}
}

      Fragment2代码:

 package com.example.fragment;

 import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.example.fragmentdemo.R; public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}

      Fragment1的xml代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF69B4"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="这是第一个Fragment" /> </LinearLayout>

      Fragment2的xml代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EECBAD"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="这是第二个Fragment" /> </LinearLayout>

      我们在activity_main.xml中添加两个Fragment,代码如下:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" > <fragment
android:id="@+id/fragment1"
android:name="com.example.fragment.Fragment1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" /> <fragment
android:id="@+id/fragment2"
android:name="com.example.fragment.Fragment2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" /> </LinearLayout>

      MainActivity代码如下:

 package com.example.fragmentdemo;

 import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

      然后运行工程就可以显示Fragment了,下面是效果图。

    

      

    2.动态添加Fragment:

      我们只需要修改MainActivity和activity_main.xml中的代码就可以了。

      activity_main.xml代码:                 

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical" > <LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" /> </LinearLayout>

      MainActivity代码:

 package com.example.fragmentdemo;

 import android.app.Activity;
import android.os.Bundle; import com.example.fragment.Fragment2; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.main, new Fragment2()).commit();
}
}

      然后运行工程就可以动态的显示Fragment了,下面是效果图。

  三、app包下和V4包下的Fragment的区别

    1、尽量不要用app包中的fragment,因为这个是在3.0之后才有的,支持的版本太高,在低版本中是是用不了的;

    2、android.support.v4.app.Fragment:可以兼容到1.6的版本;

    3、关于这两个fragment使用<fragment>标签的问题:

      (1)app.fragment和v4.fragment都是可以使用<fragment>标签的只是在在使用的时候如果是app.fragment则没有什么特殊的地方继承Activity即可。

      (2)当v4.fragment使用<fragment>标签的时候就要特别注意了:当这个Activity的布局中有<fragment>标签的时候,这个Activity必须继承FragmentActivity,否则就会报错。此时如果不卜继成FragmentActivity的话 编译系统会把<fragment>认为是app包中的Fragment来处理。但是此时我们导入的是v4包中的FragmentAndroid官方文档中的Fragment的例子就是以app包中的Fragment来讲解的。

      (3)app包中关于Fragment的类和方法在V4包中都是有相应的对应的。

    4.使用v4.fragment要通过getSupportFragmentManager()方法调用FragmentManager而不是getFragmentManager()方法。

    转载自:http://blog.csdn.net/a465456465/article/details/10415211,感谢。

      

  上面就是Fragment的简单使用方法,Demo下载,下一节我会讲Fragment的详细使用。欢迎关注,我的CSDN博客地址:http://blog.csdn.net/u010049692/article/details/38919531

      

【Android开发】之Fragment开发1的更多相关文章

  1. android开发中fragment获取context

    在用到fragment时无法使用.this来指定当前context内容,android开发中fragment获取context,可以使用getActivity().getApplicationCont ...

  2. 【Android UI设计与开发】4.底部菜单栏(一)Fragment介绍和简单实现

    TabActivity在Android4.0以后已经被完全弃用,取而代之的是Fragment.Fragment是Android3.0新增的概念,Fragment翻译成中文是碎片的意思,不过却和Acti ...

  3. Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml

    Android开发:碎片Fragment完全解析   为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...

  4. Android用户界面开发:Fragment

    Android用户界面开发:Fragment 1:注意事项  3.0以前的Android 版本要使用FragmentActivity 来装载Fragment ,使用到support v4包.  3.0 ...

  5. 【转】【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法

    原始地址:http://blog.csdn.net/yangyu20121224/article/category/1431917/1 由于TabActivity在Android4.0以后已经被完全弃 ...

  6. 【转】【Android UI设计与开发】之详解ActionBar的使用,androidactionbar

    原文网址:http://www.bkjia.com/Androidjc/895966.html [Android UI设计与开发]之详解ActionBar的使用,androidactionbar 详解 ...

  7. Android与Swift iOS开发:语言与框架对比

    Swift是现在Apple主推的语言,2014年新推出的语言,比Scala等“新”语言还要年轻10岁.2015年秋已经开源.目前在linux上可用,最近已经支持Android NDK:在树莓派上有Sw ...

  8. Android开发学习——搭建开发环境

    在学校开课学习了android的一些简单的UI组件,布局,四大组件学习了2个,数据存储及网络通信,都是一些简单的概念,入门而已.许多东西需要自己去学习. 学习一下 Android开发环境的搭建,两种方 ...

  9. 【Android UI设计与开发】第05期:引导界面(五)实现应用程序只启动一次引导界面

    [Android UI设计与开发]第05期:引导界面(五)实现应用程序只启动一次引导界面 jingqing 发表于 2013-7-11 14:42:02 浏览(229501) 这篇文章算是对整个引导界 ...

随机推荐

  1. Mininet 系列实验(一)

    关于SDN的第一个实验,似乎实验室里的前辈们也都是从这里开始的. 实验内容 使用源码安装Mininet 参考 Mininet使用源码安装 实验环境 虚拟机:Oracle VM VirtualBox U ...

  2. Java考试题之八

    QUESTION 139 Giventhe following directory structure: bigProject |--source | |--Utils.java ||--classe ...

  3. 基于excel导入数据到ms sql server

    OLE DB (Object Linking and Embedding, Database, sometimes written as OLEDB or OLE-DB) an API designe ...

  4. Webpack + React 开发 01 HelloWorld

    1.项目依赖 安装所需要依赖的其它第三方开源库,项目依赖如下: "dependencies": { "babel-core": "^6.21.0&qu ...

  5. python之旅:函数对象、函数嵌套、名称空间与作用域、装饰器

    一 函数对象 一 函数是第一类对象,即函数可以当作数据传递 #1 可以被引用 #2 可以当作参数传递 #3 返回值可以是函数 #3 可以当作容器类型的元素 二 利用该特性,优雅的取代多分支的if de ...

  6. ACM比赛_注意

    ACM比赛_注意: 比赛前: 1.前一天早一点睡觉 2.避免参加激烈的活动,以免比赛时精力不足; 3.少喝水,并提前上厕所; 4.把账号,密码都准备好,放在txt中 5.提前创建多个程序(etc.10 ...

  7. HDU--4705

    题目: Y 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4705 分析:树形dp的思想,枚举中间点. #include<iostream> ...

  8. 【位运算】判断一个数是否为2的n次方

    import java.util.Scanner; /** * 功能:用位运算,判断一个数是否为2的n次方. * 思路:用1做移位操作,然后判断移位后的值是否与给定的数相同. */ public cl ...

  9. SSH框架搭建问题总结

    1.eclipse中tomcat配置是否正确?能否在网页中访问的到? 如何在eclipse中配置tomcat就不说了,我们看下问题,在网页上访问tomcat的地址,为什么出现404错误呢? 解决办法: ...

  10. 「Vue」v-html生成的图片大小无法调整的解决办法

    问题: v-html生成的图片调整大小属性没用<div class="content" v-html="pdinfo.content"></d ...