本篇博客主要讲的是如何使用Fragment。

使用Fragment的步骤类似于自定义View的步骤:

  1. 定义Fragment的布局文件
  2. 实现扩展Fragment的子类
  3. 在扩展子类的onCreateView()方法中根据xml布局文件生成View。
  4. 在MainActivity的布局文件中引用Fragment的扩展子类。
  5. 这样运行程序就可以使用Fragment了

    代码如下(注意使用的开发环境是android studio 1.2):

    Fragmeng1的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#00FF00"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是Fragment1"
android:textColor="#000000"
android:textSize="25sp"

/>

</LinearLayout>

Fragment1对应的java类:

package com.cm.myfragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by Administrator on 2016/1/1.
*/
public class Fragment1 extends Fragment{ @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1,container,false); }
}

Fragment2的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#FFFE00"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是Fragment2"
android:textSize="25sp"
android:textColor="#000000"
/>
</LinearLayout>

Fragment2的java类:

package com.cm.myfragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by Administrator on 2016/1/1.
*/
public class Fragment2 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2,container,false);
}
}

MainActivity的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
> <fragment
android:name="com.cm.myfragment.Fragment1"
android:id="@+id/fragment1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/> <fragment
android:name="com.cm.myfragment.Fragment2"
android:id="@+id/fragment2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/> </LinearLayout>

MainActivity的java类:没有做任何改变。

运行程序的截图:

Fragment的使用(一)的更多相关文章

  1. 浅谈 Fragment 生命周期

    版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Fragment 文中如有纰漏,欢迎大家留言指出. Fragment 是在 Android 3.0 中 ...

  2. 札记:Fragment基础

    Fragment概述 在Fragment出现之前,Activity是app中界面的基本组成单位,值得一提的是,作为四大组件之一,它是需要"注册"的.组件的特性使得一个Activit ...

  3. EventBus实现activity跟fragment交互数据

    最近老是听到技术群里面有人提出需求,activity跟fragment交互数据,或者从一个activity跳转到另外一个activity的fragment,所以我给大家介绍一个开源项目,EventBu ...

  4. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  5. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  6. Android开发学习—— Fragment

    #Fragment* 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容* 生命周期方法跟Activity一致,可以理解把其为就是一个Activity* 定义布局文件作 ...

  7. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  8. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  9. Android Fragment 剖析

    1.Fragment如何产生?2.什么是Fragment Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后 ...

  10. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

随机推荐

  1. struts 头像上传

    java代码: 1 package cn.itcast.nsfw.user.action; import java.io.File; import java.io.IOException; impor ...

  2. Python unittest appium

    import unittest from appium import webdriver from appium.common.exceptions import NoSuchContextExcep ...

  3. VPN安装后报错:Reason442 & Error56

    VPN安装后一直报错,同样的32位安装包别人安装是正常,自己安装就不正常了,考虑到是自己电脑配置的问题. 经过一番努力,解决了问题,下面就本次解决过程做一个小小的总结. (1)确保VPN Servic ...

  4. ECSHOP v2.7.3注入漏洞分析和修复

    测试版本 漏洞条件 漏洞利用 产生原因 修复方案 1.测试版本 v2.7.3 RELEASE 20121106(最新) v2.7.3 RELEASE 20120411 2.漏洞条件 需登录到后台 3. ...

  5. CentOS_7.2安装Redis_3.0

    一.安装依赖包和开发工具: yum install vim vim-enhanced wget zip unzip telnet ntsysv compat* apr* nasm* gcc gcc* ...

  6. Linux主机共享目录给Windows主机的方法

    Linux主机共享目录可以通过samba来实现 首先,来看下百科上关于samba的介绍: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Ser ...

  7. 【小月博客】 Html5 上传图片 移动端、PC端通用

    在博客园注册账号有些天了,感觉有些许欣慰,自己写的东西有人在看,有人在评论很是开心.(ps: 满足一下虚荣心吧!) 废话不多说了,说一下今天给大家分享的是 html5上传图片.我们是在移动端使用的,但 ...

  8. Install Sublime Text 3

    $ sudo add-apt-repository ppa:webupd8team/sublime-text-3 $ sudo apt-get update $ sudo apt-get instal ...

  9. ARM、Intel、MIPS处理器啥区别?看完全懂了

    安卓支持三类处理器(CPU):ARM.Intel和MIPS.ARM无疑被使用得最为广泛.Intel因为普及于台式机和服务器而被人们所熟知,然而对移动行业影响力相对较小.MIPS在32位和64位嵌入式领 ...

  10. Check if a configuration profile is installed on iOS

    Configuration profiles can be downloaded to an iOS device through Safari to configure the device in ...