【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) 这篇文章算是对整个引导界 ...
随机推荐
- 【刷题】洛谷 P1402 酒店之王
题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等,也有自己所爱的菜,但是该酒店只有p间房间,一天只有固定的q道不同的菜. ...
- BZOJ 3224 普通平衡树 | 平衡树模板
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...
- AC自动机【萌新文章】
我这个蒟蒻第一次写博客,有点小激动呢. 主要是最近刚学了AC自动机,学得糟糟糕糕,记录一下,看到dalao们都在写博客,决定自己也写一波[我好水的啦,写的也不好] AC自动机大概就是 Trie+ ...
- Android O 正式版新功能
ref: Android O新特性和行为变更总结zzhttp://www.cnblogs.com/bluestorm/p/7148134.html Android O正式版带来了诸多新功能,如Tens ...
- Python之旅:流程控制
流程控制之if...else 每条if语句的核心都是一个值为True或False的表达式,这种表达式被称为条件测试. Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定 ...
- Jenkins(三)---Jenkins初始配置和插件配置
从Jenkins(二)中可以知道 jenkins 的工作目录为/opt/jenkins [很重要!!!][很重要!!!][很重要!!!]在配置此目录以前,将这两台的主机进行配置为ssh root用户无 ...
- 关于strassen矩阵乘法的矩阵大小不是2^k的形式时,时间复杂度是否还是比朴素算法好的看法
原来是n,找到大于等于n且是2^k形式的数m.n*n的矩阵补全为m*m的矩阵,原来的矩阵放在最左上方,其它位置的值为0.朴素方法:n^3现在:m^2.8即m/n需小于e^(3/2.8)=2.919才能 ...
- 二、Linux学习之centOS的的setup
因为我是在VMware上安装的centOS,网络设置选择的是桥接模式,这样可以和实体机使用同样的网络,但是也使用同样的IP,因此就需要设置一下IP了,否则使用ifconfig查询ip的时候显示的是12 ...
- SMO详解
转自:简书https://www.jianshu.com/p/55458caf0814 SVM通常用对偶问题来求解,这样的好处有两个:1.变量只有N个(N为训练集中的样本个数),原始问题中的变量数量与 ...
- Java基础-IO流对象之File类
Java基础-IO流对象之File类 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.IO技术概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下 ...