• 简介

    在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. Delphi 接口托管实现

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  2. (原)下载pubFig的python代码

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5715305.html pubFig数据库网址: http://www.cs.columbia.edu/ ...

  3. tomact虚拟目录,虚拟主机,http请求头,相应头

    tomact虚拟目录,虚拟主机,http请求头,相应头 07. 五 / J2EE / 没有评论   一.服务器,容器(软件)1.服务器:提供网络访问的程序2.容器:支持什么技术的服务器就叫做什么容器. ...

  4. setTimeout的时间设为0的问题

    javascript是单线程执行的,当某一段代码正在执行的时候,所有的后续任务都必须等待,形成一个队列, 一旦当前任务执行完毕,再从队列中取出下一个任务.这常被称为”阻塞式执行“. 如果代码中设定一个 ...

  5. PXE简要配置过程

    目录 1.所需服务 2.简要配置过程     1.dhcp服务     2.tftp服务     3.提供pxelinux.0配置文件     4.提供系统所需文件 1.所需服务:     dhcp服 ...

  6. 负载均衡集群之LVS持久链接

    原理--> 通过构建一个hash表,利用CIP与RS的对应关系,来保持来自一个CIP的各种服务都走同一个RS 目的--> 保持持久链接的同时,将多个服务合并起来,例如http和https ...

  7. information_schema.triggers 学习

    mysql实例中的每一个trigger 对应到information_schema.triggers 中有一行 1.information_schema.triggers 表的常用列: 1.trigg ...

  8. 8051_asm.uew

    /L20"8051 Assembly" AASM_LANG Line Comment = ; Nocase String Chars = ' File Extensions = S ...

  9. 2013Q1全球网速排名 韩国第1美国第9 中国呐?(图)

    德国著名统计公司Statista,最近公布了2013 Q1季度全球互联网网速排名数据.其中,韩国以平均14.2Mbps的网速继续排名世界第一. 此外,第二至第十名的国家和地区分别为:日本(11.7 M ...

  10. 更新ORACLE数据时遇到锁死情况的处理

    我们在操作数据库的 时候,有时候会由于操作不当引起数据库表被锁定,这么我们经常不知所措,不知怎么给这些表解锁,在pl/sql Developer工具的的菜单“tools”里面的“sessions”可以 ...