Android Fragment是Android4.0以上才有的;而FragmentActivity是为了兼容4.0以下版本的Fragment使用的。

所以如果你想兼容4.0以下Android版本使用Fragment的话,框架Activity需要继承FragmentActivity,FragmentActivity这个类是在android.support.v4.app.FragmentActivity里的。

下面介绍2种用法:

1、继承Activity的。

(这个只针对4.0以上的Android平台使用Fragment)。

框架Activity:

package com.tandong.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 不兼容4.0以下模式Fragment
 * 
 * @author tandong
 * 
 */

public class Mt_Activity extends Activity implements OnClickListener {
    private Button btn_first, btn_second;
    private Fragment Fragment_first, Fragment_Second;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment);
        initView();

Fragment_Second = new Fragment_Second();
        fragmentManager = this.getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_Second,"second_Fragment");
        fragmentTransaction.commit();
    }

private void initView() {
        btn_first = (Button) this.findViewById(R.id.btn_first);
        btn_second = (Button) this.findViewById(R.id.btn_second);
        btn_first.setOnClickListener(this);
        btn_second.setOnClickListener(this);
    }

@Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.btn_first:
            // 加载不同的Fragment
            if (null == Fragment_first) {
                Fragment_first = new Fragment_first();
            }
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_first, "fist_Fragment");
            fragmentTransaction.commit();
            break;
        case R.id.btn_second:
            if (null == Fragment_first) {
                Fragment_Second = new Fragment_Second();
            }
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_Second, "second_Fragment");
            fragmentTransaction.commit();
            break;
        default:
            break;
        }

}

}

Fragment代码:

package com.tandong.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_Second extends Fragment {
    private View rootView;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(container==null)
           return null;
        rootView = inflater.inflate(R.layout.fragment_two, container,false);

return rootView;
    }
}

2.继承FragmentActivity的(向下兼容4.0以下版本使用Fragment,导入的是android.support.v4包里的内容)

框架Activity:

package com.tandong.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 兼容4.0以下模式Fragment
 * 
 * @author tandong
 * 
 */

public class Mt_Activity extends FragmentActivity implements OnClickListener {
    private Button btn_first, btn_second;
    private Fragment Fragment_first, Fragment_Second;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment);
        initView();

Fragment_first = new Fragment_first();
        fragmentManager = this.getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_first,"first_Fragment");
        fragmentTransaction.commit();
    }

private void initView() {
        btn_first = (Button) this.findViewById(R.id.btn_first);
        btn_second = (Button) this.findViewById(R.id.btn_second);
        btn_first.setOnClickListener(this);
        btn_second.setOnClickListener(this);
    }

@Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.btn_first:
            // 加载不同的Fragment
            if (null == Fragment_first) {
                Fragment_first = new Fragment_first();
            }
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_first, "fist_Fragment");
            fragmentTransaction.commit();
            break;
        case R.id.btn_second:
            if (null == Fragment_first) {
                Fragment_Second = new Fragment_Second();
            }
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.main_fragment_layout, Fragment_Second, "second_Fragment");
            fragmentTransaction.commit();
            break;
        default:
            break;
        }

}

}

Fragment代码:

package com.tandong.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment_first extends Fragment {
    private View rootView;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(container==null)
           return null;
        rootView = inflater.inflate(R.layout.fragment_first, container,false);

return rootView;
    }
}

最后再说一句布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<LinearLayout
        android:id="@+id/top_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

<Button
            android:id="@+id/btn_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/top_bar_bg"
            android:text="按钮一" />

<Button
            android:id="@+id/btn_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/top_bar_bg"
            android:text="按钮二" />

</LinearLayout>

<LinearLayout
        android:id="@+id/fragment_replace_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/top_bar_layout"
        android:background="#ff0000" >
    </LinearLayout>

</RelativeLayout>

布局类似这种布局即可,并不是一定非要FrameLayout

Android Fragment和FragmentActivity区别和用法的更多相关文章

  1. android Fragment和FragmentActivity

    MainActivity.java import android.app.AlertDialog; import android.app.Notification; import android.co ...

  2. Android Fragment用法详解(2)--动态添加Fragment

    在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...

  3. Android Fragment用法知识点的讲解

    Android Fragment用法的讲解 碎片,它的出现是为了更好展示UI的设计,让程序更加得到充分的展示.Fragment的出现,如微信的额主界面包含多个Fragment,使得微信功能更加简洁明了 ...

  4. Android Fragment 真正的完全解析

    出处: 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragment谈上关系,做什么都要问下Fragment能实现不~~~哈哈,是不是有点过~~~ 本篇博客力求为大家说明Fragmen ...

  5. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

  6. Android Fragment 解析和使用

    Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transactio ...

  7. Android Fragment 你应该知道的一切

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42628537,本文出自:[张鸿洋的博客] 很久以前写过两篇Fragment的介绍 ...

  8. Android Fragment详解

    一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...

  9. 转 Fragment 和 FragmentActivity的使用

    今天学习下 Android中的 Fragment 和 FragmentActivity,因为没有4.0手机,平台是2.3.3 所以我是使用 v4 support 包来进行学习. 要想用Fragment ...

随机推荐

  1. maven项目中 org.hibernate.MappingNotFoundException: resource:*.hbm.xml not found问题的解决方案

    是因为*.hbm.xml没有放到resource的mapper下导致的 对于Maven工程,编译的工作是由Maven程序来完成的,而Maven默认只会把src/main/resources文件夹下的文 ...

  2. Express4.x API (三):Response (译)

    Express4.x API 译文 系列文章 Express4.x API (一):application (译) -- 完成 Express4.x API (二):request (译) -- 完成 ...

  3. 收藏pdf 链接

    python 入门: https://files.cnblogs.com/files/minsons/python%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E6%B7% ...

  4. 微服务之Sping Cloud

    版本说明 Finchley SR2 价值简要 微服务之间是松耦合,跨不同业务部门,提供非常充分的灵活性,加快项目开发完成效率,方便组件化独立可扩展性及复用. 微服务应用结构表现 组件简要 1. Eur ...

  5. Redis常用操作-------Set(集合)

    1.SADD key member [member ...] 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略. 假如 key 不存在,则创建一个 ...

  6. 软件工程M1/M2总结

    也不分M1/M2了,就从头到尾的梳理一下这学期的软工课吧. 第一节课,老师就稀里哗啦说了一下这学期要怎么搞,什么个人项目啦,结对项目啦,团队项目一二啦,还要组队啊什么的,然后风风火火的组队. 个人项目 ...

  7. [福大软工] Z班——Alpha现场答辩情况汇总

    Alpha现场答辩 小组互评(文字版) 各组对于 麻瓜制造者 的评价与建议 队伍名 评价与建议 *** 界面较友好,安全性不足,功能基本完整.希望能留下卖家的联系方式而不是在APP上直接联系,APP上 ...

  8. 注解Annotation

    @java.lang.annotation.Target(value={java.lang.annotation.ElementType.TYPE}) @java.lang.annotation.Re ...

  9. ajax 异步请求 代码

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. Python文件os模块

    一.文件操作 1.打开一个文件 fo = open("foo.txt", "wb") fo.write( "www.runoob.com!\nVery ...