Fragment的使用(一)
本篇博客主要讲的是如何使用Fragment。
使用Fragment的步骤类似于自定义View的步骤:
- 定义Fragment的布局文件
- 实现扩展Fragment的子类
- 在扩展子类的onCreateView()方法中根据xml布局文件生成View。
- 在MainActivity的布局文件中引用Fragment的扩展子类。
- 这样运行程序就可以使用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的使用(一)的更多相关文章
- 浅谈 Fragment 生命周期
版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Fragment 文中如有纰漏,欢迎大家留言指出. Fragment 是在 Android 3.0 中 ...
- 札记:Fragment基础
Fragment概述 在Fragment出现之前,Activity是app中界面的基本组成单位,值得一提的是,作为四大组件之一,它是需要"注册"的.组件的特性使得一个Activit ...
- EventBus实现activity跟fragment交互数据
最近老是听到技术群里面有人提出需求,activity跟fragment交互数据,或者从一个activity跳转到另外一个activity的fragment,所以我给大家介绍一个开源项目,EventBu ...
- Android:Activity+Fragment及它们之间的数据交换.
Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android开发学习—— Fragment
#Fragment* 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容* 生命周期方法跟Activity一致,可以理解把其为就是一个Activity* 定义布局文件作 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Android中Fragment的两种创建方式
fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...
- Android Fragment 剖析
1.Fragment如何产生?2.什么是Fragment Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
随机推荐
- 无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用。
在删除northwindcs表时,发生报错,消息 3726,级别 16,状态 1,第 2 行,无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用.此时判断是因为有其他表的外键 ...
- Java界面
JFrame.java package myProject; import java.awt.Color;import java.awt.EventQueue; import javax.swing. ...
- 开机报这个错误,然后 adobe软件无法使用
开机报这个错误,然后 adobe软件无法使用 按照此方法测试,无效,在原基础如下更改 /tmp 是连接文件找到根文件(/private/tmp)删除重新给予权限,重新建立连接恢复正常 使用到的命令 ...
- 在Spring里进行单元测试Junit
搭建Spring环境(自行搭建): @RunWith注解指定使用springJunit的测试运行器 @ContextConfiguration注解指定测试用的spring配置文件的位置 import ...
- 【转】OPENGL基本API概述
本文信息资源来源于网络,欢迎转载!转载时请保留本文链接(http://www.shopliyang.com.cn/)! OpenGL中的gl库是核心库,glu是实用库,glut是实用工具库. gl是核 ...
- ubuntu 跟xshell的问题
有2个分析: 1:是windos的防火墙没有关闭 2:是虚拟机没有安装sshd服务器 ubuntu在CLI界面下输入:dpkg -l |grep ssh 因为是我安装过的sshd server 要 ...
- 数字图像处理作业使用OpenCV - 使用笔记
数字图像处理作业的输入图像全部都是灰度图像,所以汇总一下自己遇到的问题答案. OCV的图像容器是Mat<typename>,可以用imread(filename)读取图像,filename ...
- js判断数据类型
1.判断一个数字是否是无穷的 isFinite()例:var aa=Number.POSITIVE_INFINITY; if(isFinite(aa)){ alert("aa不是无穷的&qu ...
- git pull提示当前不在某个分支上
$ git pull You are not currently on a branch, so I cannot use any 'branch.<branchname>.merge' ...
- LINUX磁盘分区、格式化、挂载、卸载全程详解
1.一切皆文件 Linux系统有一个理念:“一切皆文件”,所以计算机的硬件在linux中也是以“文件”的形式存在于/dev目录中. 图为CentOS 6.5系统中/dev目录的部分内容.不同的计算 ...