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

最新文章

  1. 9.Struts2在Action中获取request-session-application对象
  2. Intent传递list&lt;bean&gt;集合
  3. 《与mysql零距离接触》视屏学习笔记
  4. MySql执行多条语句
  5. linux设置tomcat开机启动
  6. java7-4 继承的练习
  7. matlab中图像显示函数
  8. innodb数据库批量转换表引擎为MyISAM
  9. VB.net 字符串 分割 及 重新倒序组装
  10. JDBC 简介
  11. Impala入门笔记
  12. Okhttp3日志采集功能
  13. 用EnableMenuItem不能使菜单变灰的原因
  14. gameUnity 0.2 网络游戏框架(计划)
  15. php隔行换色输出表格
  16. 如何开发webpack loader
  17. 数位dp-Bomb
  18. nio、bio区别,应运场景
  19. [java大数据面试] 2018年4月百度面试经过+三面算法题:给定一个数组,求和为定值的所有组合.
  20. Docker部署Consul集群

热门文章

  1. C#一些常用的图片操作方法:生成文字图片 合并图片等
  2. ngx_lua 模块
  3. webpack教程(一)——初体验
  4. SpringBoot笔记--FastJson
  5. 线上mongodb 数据库用户到期时间修改的操作记录
  6. Visual
  7. Hibernate_core_method
  8. 2017BUAA软工个人作业Week1
  9. Windows samba history
  10. [转帖] linux下面 vim 数字键无法插入的解决办法