首先新建一个fragment的布局文件,
 
<?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:gravity="center">

<Button
        android:id="@+id/btnLeftFrag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button in left fragment" />

</LinearLayout>

 
然后在新建控制该fragment的类,该类继承自Fragment。
 
package com.example.flypie.notesbook.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.flypie.notesbook.R;

/**
 * Created by FLYPIE on 2015/12/11.
 */
public class LeftFragment extends Fragment{

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

 
在需要使用该fragment的布局中加入
 
<fragment
    android:id="@+id/fragLeft"
    android:name="com.example.flypie.notesbook.Fragment.LeftFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    tools:layout="@layout/left_fragment" />
 
注意:加载布局时会执行android:name指定的类
 
Fragment与所在Activity之间通信:
 
在Activity中可以通过以下方法更改不同的Fragment:
 
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.rightLayout,anotherRightFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
 
注意:其中的R.id.rightLayout是包含当前要更换的fragment的容器
 
在Fragment中可以通过以下方法获取所在Activity的实例,进而调用其方法:
 
TestFragmentActivity testFragmentActivity = (TestFragmentActivity) getActivity();
 
可以通过调用Activity的以下方法获取指定的fragment的实例,进而调用其方法:
 
RightFragment rightFragment = (RightFragment) testFragmentActivity.getFragmentManager().findFragmentById(R.id.fragRight);
 
 

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

  1. 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿

    腾讯优测是专业的android自动化测试平台,拥有上千款真机,彻底解决android碎片化问题! 这里我要说的不是闪光灯的硬件特征,也不是说底层驱动的原理,我只是跟大家聊一聊在项目中遇到的一些关于闪光 ...

  2. Android碎片(Fragment)简述

    碎片(Fragment)是一种可以嵌入活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因此碎片在平板上的应用非常广泛. 你可以将碎片理解成一个迷你型的活动,水平同样可能包含布局,同样都 ...

  3. android: 碎片的demo

    现在你已经将关于碎片的重要知识点都掌握得差不多了,不过在灵活运用方面可能还有 些欠缺,因此又该进入我们本章的最佳实践环节了. 前面有提到过,碎片很多时候都是在平板开发当中使用的,主要是为了解决屏幕空间 ...

  4. android:碎片的生命周期

    和活动一样,碎片也有自己的生命周期,并且它和活动的生命周期实在是太像了,我相 信你很快就能学会,下面我们马上就来看一下. 4.3.1    碎片的状态和回调 还记得每个活动在其生命周期内可能会有哪几种 ...

  5. android:碎片的使用方式

    介绍了这么多抽象的东西,也是时候应该学习一下碎片的具体用法了.你已经知道,碎 片通常都是在平板开发中才会使用的,因此我们首先要做的就是新建一个平板电脑的模拟 器.由于 4.0 系统的平板模拟器好像存在 ...

  6. Fragment、Activity比较——Android碎片介绍

    Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似,下面介绍下Android Fragment的作用和用法.Fragmen ...

  7. 安卓Android碎片fragment实现静态加载

    静态加载好后的界面如下,两个碎片分别位于一个活动的左边和右边: 左边和右边分别为一个碎片,这两个碎片正好将一整个活动布满.一个活动当中可以拥有多个碎片,碎片的含义就是可以在同一个UI界面下,将这个界面 ...

  8. android:碎片的概念

    碎片(Fragment)是一种可以嵌入在活动当中的 UI 片段,它能让程序更加合理和充分 地利用大屏幕的空间,因而在平板上应用的非常广泛.虽然碎片对你来说应该是个全新的概 念,但我相信你学习起来应该毫 ...

  9. Android——碎片事务调用失败

    遇到一个情况: 在一个定时器中,每秒发起一次网络请求查询订单支付状态,如果支付成功,则在response中发出一条信息: handler中收到信息,则控制碎片切换. 结果遇到了一个错误:就是碎片切换事 ...

随机推荐

  1. Saving changes is not permitted in SQL Server

    From Save (Not Permitted) Dialog Box on MSDN : The Save (Not Permitted) dialog box warns you that sa ...

  2. git设置hooks 钩子

    github是可以设置hooks的,看:在设置webhooks & services,可在Just the push event.是设定向你的服务器发请求,然后再做相应的处理. https:/ ...

  3. oracle数据库安装

    1.oracle10g下载完成后,选择“setup.exe”启动安装. 2.oracle主目录位置就是oracle准备安装的位置,称为"Oracle_Home".Oracle安装的 ...

  4. JS中的prototype///////////////////////////z

    JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...

  5. 5.echo(),print(),print_r()的区别

    echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print()    只能打印出简单类型变量的值(如int,string) print_r() ...

  6. 2.在程序中如何实现Cookie信息的设置,读取和删除

    设置:你可以在IE的“工具/Internet选项”的“常规”选项卡中,选择“设置/查看文件”,查看所有保存到你电脑里的Cookies.这些文件通常是以user@domain格式命名的,user是你的本 ...

  7. paper 113:Bhattacharyya distance

    在统计理论中,Bhattacharyya距离用来度量两个离散或连续概率分布的相似性.它与Bhattacharyya系数(Bhattacharyya coefficient)高度相关,后者是用来度量两个 ...

  8. spring使用elasticsearch 5.x

    elasticsearch客户端选择 这里使用transport建立elasticsearch客户端 applicationContext.xml配置,属性可以采用读取属性文件的方式.参考类Prope ...

  9. REPL LOG

    using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...

  10. .NET 使用CouchBase 基础篇

    2011年2月,CouchOne和memebase合并后,改名为Couchbase,官网地址(www.couchbase.com).membase最后一个版本为1.7.2,可在Couchbase的官网 ...