Android Fragment和FragmentActivity区别和用法
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:
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代码:
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:
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代码:
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;
}
}
最后再说一句布局:
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区别和用法的更多相关文章
- android Fragment和FragmentActivity
MainActivity.java import android.app.AlertDialog; import android.app.Notification; import android.co ...
- Android Fragment用法详解(2)--动态添加Fragment
在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...
- Android Fragment用法知识点的讲解
Android Fragment用法的讲解 碎片,它的出现是为了更好展示UI的设计,让程序更加得到充分的展示.Fragment的出现,如微信的额主界面包含多个Fragment,使得微信功能更加简洁明了 ...
- Android Fragment 真正的完全解析
出处: 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragment谈上关系,做什么都要问下Fragment能实现不~~~哈哈,是不是有点过~~~ 本篇博客力求为大家说明Fragmen ...
- Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...
- Android Fragment 解析和使用
Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transactio ...
- Android Fragment 你应该知道的一切
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42628537,本文出自:[张鸿洋的博客] 很久以前写过两篇Fragment的介绍 ...
- Android Fragment详解
一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...
- 转 Fragment 和 FragmentActivity的使用
今天学习下 Android中的 Fragment 和 FragmentActivity,因为没有4.0手机,平台是2.3.3 所以我是使用 v4 support 包来进行学习. 要想用Fragment ...
随机推荐
- Object-Oriented(二)原型对象
自用备忘笔记 1. 理解原型对象 只要创建函数,函数上就会创建一个 prototype 属性指向函数的原型对象. function Person() {} Person.prototype //指向该 ...
- React Native 教程:001 - 如何运行官方控件示例 App
原文发表于我的技术博客 本文主要讲解了如何运行 React Native 官方控件示例 App,包含了一些 React Native 的基础知识以及相关环境的配置. 原文发表于我的技术博客 React ...
- systemctl添加开机启动
我们对service和chkconfig两个命令都不陌生,systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体. systemctl is-enable ...
- WinForm多线程+委托防止界面假死
当有大量数据需要计算.显示在界面或者调用sleep函数时,容易导致界面卡死,可以采用多线程加委托的方法解决 using System; using System.Collections.Generic ...
- week4--系统调用的工作机制
潘恒 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.使用库函数AP ...
- Linux内核设计与实现 第三章
1. 进程和线程 进程和线程是程序运行时状态,是动态变化的,进程和线程的管理操作都是由内核来实现的. Linux中的进程于Windows相比是很轻量级的,而且不严格区分进程和线程,线程不过是一种特殊的 ...
- 用户模拟+spec
用户模拟:用户模拟的对象分别为小学二年级学生,学生父亲以及小学教师. 名字 孔小颖 性别 男 职业 小学生 收入 来自父母给予 知识层次和能力 小学二年级 生活/工作情况 在学校上课,数学成绩较差,放 ...
- [转帖]知乎专栏:正确使用 Docker 搭建 GitLab 只要半分钟
正确使用 Docker 搭建 GitLab 只要半分钟 https://zhuanlan.zhihu.com/p/49499229 很多程序员在内网搭建 gitlab 都搭建的坑坑洼洼,不支持 htt ...
- [转帖]Intel新一代Xeon完整曝光
AMD已经官宣7nm工艺的第二代EPYC霄龙服务器平台,今年上半年就会大规模出货,而在Intel这边,由于10nm工艺进展还是不够快,在服务器上还是需要14nm继续打天下,而且还有两代14nm工艺产品 ...
- auto_increment 自增键的一些说明
导致auto_increment变小的几种情况: 1. alter table xx auto_increment = yy; 2. truncate table 3. restart mysql 第 ...