首先新建一个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. AndroidStudio2.0开发环境搭建

    实验内容 下载安装Android Studio 配置使用环境 实验要求 下载安装Android Studio 基本环境配置 实验步骤 1.首先从安卓开发者网站下载最新的Android Studio h ...

  2. [Android Tips] 2. Disable recent apps dialog on long press home button

    public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Log.d(&qu ...

  3. Java基础之在窗口中绘图——绘制星星(StarApplet 1)

    Applet程序. 可以把更复杂的几何形状定义为GeneralPath类型的对象.GeneralPath可以是直线.Quad2D曲线和Cubic2D曲线的结合体,甚至可以包含其他GeneralPath ...

  4. Spring框架简介 Spring Framework Introduction

    Introduction The Spring Framework provides a comprehensive programming and configuration model for m ...

  5. 响应式布局1--媒体查询和-webkit-min-device-pixel-ratio

    -webkit-min-device-pixel-ratio其实就是这个玩意 window.devicePixelRatio是设备上物理像素和设备独立像素(device-independent pix ...

  6. node与Elment以及子节点childrenNode与children的区别(2)

    测试代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. Mysql 获取当前时间函数 (类似于sql server 中的 getDate())

    1 获得当前日期+时间(date + time)函数:now() 2 获得当前日期+时间(date + time)函数:sysdate() sysdate() 日期时间函数跟 now() 类似,不同之 ...

  8. IPC

    IPC,全名Inter Process Communication即进程间通讯,在同一台机器上的两个进程就用IPC,不能跨物理机器。IPC包括共享内存、队列、信号量等几种方式,由于IPC通讯效率之高, ...

  9. 为什么要加 -moz- -webkit- -ms- -o- ?

    没有别的,为了兼容早期版本,为了解决CSS3标准正式发布以前的遗留问题.

  10. CRM 2011 Install Errors - Tips and Tricks continued(转)

    The more I get to install/upgrade to CRM 2011 in different environment the more I come across differ ...