• 简介

    在Android中Fragment为一种可以嵌入活动中的UI片段.能让程序更加合理地利用大屏幕的空间.
  • 使用方法

    1.我们首先新建的一个onefragment.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="#e3e3e3"
android:orientation="vertical" > <TextView
android:id="@+id/tv_right_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="this is fragment"
/>
</LinearLayout>

2.新建一个OneFragment.class(这里要注意的是,不要继承V4包)

public class OneFrament extends Fragment{

	@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.another_right_fragment, container, false);
return view;
}
}

3.使用OneFrament

<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmentdemo.OneFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
  • 动态添加Fragment.

    1.在要添加到的activity.xml布局里面添加.
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" >
</FrameLayout>

2.在要添加到的activity.class里添加.

AnotherRightFrament frament=new AnotherRightFrament(); //实例化要添加的Fragment
FragmentManager fragmentManager=getFragmentManager(); //取到FragmentManager.
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, frament); //根据ID指定要添加到的FrameLayout
transaction.commit();
  • 在上面.

    1.创建待添加的碎片实例.

    2.获取到FragmentManager,在活动中可以直接调用getFramentManager()方法得到.

    3.开启一个事务,通过调用beginTransaction()方法开启.

    4.向容器内添加碎片,一般使用replace()方法,需要传入容器的ID和待添加的碎片实例.

    5.提交事务,调用commit()方法来完成.

更......

  • 当我们运行上面的程式,跳转到另一个fragment的时候,习惯性的按下back返回,当却发现程序退出了.这个时候我们就要在碎片中模拟返回栈.

    transaction.addToBackStack(null);
AnotherRightFrament frament=new AnotherRightFrament();
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, frament);
transaction.addToBackStack(null);
transaction.commit();

我们在事务提交之前调用了FragmentTransaction的addToBackStack()方法,他可以接受一个名字用于描述返回栈的状态,一般传入null就可以了.

  • 碎片和活动之间进行通信
getFragmentManager().findFragmentById(R.id.one_fragment);    //android提供了这样一种方式,一个类似于findviewbyid()的方法,专门来从布局文件获取fragment的实例.
MainActivity activity=(MainActivity) getActivity();    //android 提供了getactivity()来得到和当前碎片有关的activity实例.

Android Fragment(碎片)的使用的更多相关文章

  1. Android Fragment碎片

    什么是碎片? 碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛.可以把Fragment当成Activity一个界面的一 ...

  2. Android Fragment应用实战,使用碎片向ActivityGroup说再见

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...

  3. Android Fragment完全解析,关于碎片你所需知道的一切

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...

  4. Android Fragment完全解析,关于碎片你所需知道的一切 (转)。

    我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activity也有它的局限性,同样的界面在手机 ...

  5. Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml(转)

    注明:这个转的,见谅未能标明原始出处 我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activ ...

  6. Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml

    Android开发:碎片Fragment完全解析   为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...

  7. (转) Android Fragment完全解析,关于碎片你所需知道的一切

    我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activity也有它的局限性,同样的界面在手机 ...

  8. Android Fragment应用实战

    现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...

  9. Android Fragment

    1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...

随机推荐

  1. GDI 开发的准备工作

    1 需要的链接库和头文件 大部分函数在 Gdi.dll 和 Gdi32.dll 提供.相关的函数接口和结构都在 Wingdi.h 文件中(如果工程中已包含 Windows.h 就不需要再包含了,因为 ...

  2. 在word中使用notepad++实现代码的语法高亮

    转载自:http://blog.csdn.net/woohello/article/details/7621651 有时写文档时需要将代码粘贴到word中,但直接粘贴到word中的代码虽能保持换行与缩 ...

  3. Azure上A/D系列虚拟机到DS系列迁移(1)

    有一些用户在刚开始的时候创建了A7,D14等虚拟机来跑一些IO要求比较高的应用,比如Oracle数据库,MySQL数据库,等到用户量上来之后,性能不够,需要升级数据磁盘到SSD固态硬盘,但是问题是: ...

  4. java通过jdbc连接mysql数据库

    下载mysql驱动包: http://dev.mysql.com/downloads/file.php?id=456317 解压之后里面有个mysql-connector-java-5.1.35-bi ...

  5. ubuntu 12.04(Precise Pangolin)启用休眠(Hibernate)功能的方案

    官方支持文档:https://help.ubuntu.com/12.04/ubuntu-help/power-hibernate.html 按照这篇官方支持,因为休眠功能存在bug,12.04没有默认 ...

  6. kindeditor在sae上传文件修改,适合php

    kindeditor在sae上传文件修改,适合php 当前位置: 首页  > 论坛  > 经验共享 用户登录   新用户注册   主题: kindeditor在sae上传文件修改,适合ph ...

  7. CDH 2、Cloudera Manager的安装

    1.Cloudera Manager • Cloudera Manager是一个管理CDH的端到端的应用. • 作用: – 管理 – 监控 – 诊断 – 集成 • 架构 • Server – 管理控制 ...

  8. AC大牛经典语录

    超经典:     1. 为了世界的和平,为了女生的安全,我拼命做题,做题,做题! 2. A ac a day, keeps the doctor away! 3. from good to great ...

  9. STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)

    向量(vector) <vector> 连续存储的元素<vector> Vector<int>c; c.back()    传回最后一个数据,不检查这个数据是否存在 ...

  10. Innobackupex 全备数据库

    对于MySQL数据库的热备.xtrabackup是除了MySQL enterprise backup之外的不二之选. 该工具提供了基于innodb存储引擎的热备.支持全量,增量备份,部分备份,时点恢复 ...