• 简介

    在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. TCP/IP 3次握手

    参看下面链接:http://blog.chinaunix.net/uid-20665047-id-3137792.html

  2. vs2010中的外部依赖项的含义

    vs2010中的项目下拉列表下面有外部依赖项,里面显示的文件是你程序中显示包含的头文件所包含的的头文件.比如,你的main函数里面包含了windows.h头文件,而windows.h头文件又包含其他头 ...

  3. c#或获取系统的特殊路径,如我的文档等

    Console.WriteLine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); System.E ...

  4. [原]C++程序示例:涉及到抽象类、继承…

    C++复习题. 有助于理解: .h与.cpp分离 基类.派生类.抽象类 多态.纯虚函数 对象指针.引用 派生类使用基类的构造函数 ... =============================== ...

  5. [WPF]ListView点击列头排序功能实现

    [转]   [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...

  6. Asp.net 处理程序(第五篇)

    HttpApplication有19个标准事件,当到达第8个事件PostMapRequestHandler触发的时候,标志着已经获取到了处理请求的处理程序对象,在第11个事件PreRequestHan ...

  7. python模块学习之random

    模块源码: Source code: Lib/random.py 文档:http://docs.python.org/2/library/random.html 常用方法: random.random ...

  8. 【转】飞凌嵌入式(Forlinx)TE/OK6410内核编译:“make: arm-none-linux-gnueabi-gcc:命令未找到”

    原文网址:http://www.xuebuyuan.com/1104711.html Ubuntu10.04下编译飞凌嵌入式(Forlinx)TE/OK6410开发板提供的内核2.6.36 本以为按照 ...

  9. Best Time to Buy and Sell Stock III 解答

    Question Say you have an array for which the ith element is the price of a given stock on day i. Des ...

  10. Java Service Wrapper

    Java Service Wrapper 将Java 应用程序部署成Windows系统服务Java Service Wrapper 1 Product Overview 1 Editions 2 Me ...