【Android开发】之Fragment开发1
一直知道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的更多相关文章
- android开发中fragment获取context
在用到fragment时无法使用.this来指定当前context内容,android开发中fragment获取context,可以使用getActivity().getApplicationCont ...
- 【Android UI设计与开发】4.底部菜单栏(一)Fragment介绍和简单实现
TabActivity在Android4.0以后已经被完全弃用,取而代之的是Fragment.Fragment是Android3.0新增的概念,Fragment翻译成中文是碎片的意思,不过却和Acti ...
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml
Android开发:碎片Fragment完全解析 为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...
- Android用户界面开发:Fragment
Android用户界面开发:Fragment 1:注意事项 3.0以前的Android 版本要使用FragmentActivity 来装载Fragment ,使用到support v4包. 3.0 ...
- 【转】【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法
原始地址:http://blog.csdn.net/yangyu20121224/article/category/1431917/1 由于TabActivity在Android4.0以后已经被完全弃 ...
- 【转】【Android UI设计与开发】之详解ActionBar的使用,androidactionbar
原文网址:http://www.bkjia.com/Androidjc/895966.html [Android UI设计与开发]之详解ActionBar的使用,androidactionbar 详解 ...
- Android与Swift iOS开发:语言与框架对比
Swift是现在Apple主推的语言,2014年新推出的语言,比Scala等“新”语言还要年轻10岁.2015年秋已经开源.目前在linux上可用,最近已经支持Android NDK:在树莓派上有Sw ...
- Android开发学习——搭建开发环境
在学校开课学习了android的一些简单的UI组件,布局,四大组件学习了2个,数据存储及网络通信,都是一些简单的概念,入门而已.许多东西需要自己去学习. 学习一下 Android开发环境的搭建,两种方 ...
- 【Android UI设计与开发】第05期:引导界面(五)实现应用程序只启动一次引导界面
[Android UI设计与开发]第05期:引导界面(五)实现应用程序只启动一次引导界面 jingqing 发表于 2013-7-11 14:42:02 浏览(229501) 这篇文章算是对整个引导界 ...
随机推荐
- Redis学习 - 主从拷贝
Redis主从拷贝的特点 同一个Master可以拥有多个Slaves. Master下的Slave还可以接受同一架构中其它slave的链接与同步请求,实现数据的级联复制,即Master->Sla ...
- 【bzoj3751】 Hnoi2014—画框
http://www.lydsy.com/JudgeOnline/problem.php?id=3571 (题目链接) 题意 给出一个$2*N$个点的二分图,$N*N$条边,连接$i$和$j$的边有两 ...
- 【DP/数学】【CF1061C】 Multiplicity
Description 给定一个序列 \(a\),求有多少非空序列 \(b\) 满足 \(b\) 是 \(a\) 的子序列并且 \(\forall~k~\in~[1,len_b],~~k \mid b ...
- pg_upgrade升级报错:Only the install user can be defined in the new cluster
前两天pg11刚出来,打算测试一下,想将测试库升级到pg11,之前测试库的版本是pg9.6,后面我将它升到了pg10,打算在pg10的版本基础上升级到pg11. 但执行时,多次报出: Performi ...
- lumen 使用 dingo API 在 phpunit 中 404 的解决方法, 以及鉴权问题
1. phpunit.xml 中添加 dingo 相关配置 <env name="API_STANDARDS_TREE" value="x"/> & ...
- java反射获取class
- IL指令集
声明: 1.本指令集搜集自网上各个论坛帖子,欢迎补充 IL指令集 名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推 ...
- Python【datetime】模块
import datetimeprint("==============date类================")#创建一个date对象:datetime.date(year, ...
- Ansible1: 简介与基本安装
目录 Ansible特性 Ansible的基本组件 Ansible工作机制 Ansible的安装 Ansible是一个综合的强大的管理工具,他可以对多台主机安装操作系统,并为这些主机安装不同的应用程序 ...
- 如何使用vuejs过滤器
大家再使用vue做项目时,查询功能当然必不可少,这就得使用vue强大的filter啦.其实vue内置的两个属性filterBy和orderBy已经能满足部分需求了,但是她更大的的魅力在于自定义filt ...