参考自张泽华视频

Fragment是自Android3.0后引入的特性,主要用于在不同的屏幕尺寸中展现不同的内容。

Fragment必须被嵌入Activity中使用,总是作为Activity的组成部分。

简单示例:

一个Activity的界面由2个部分组成,每个部分分别是一个Fragment。

效果图如下:

1、创建第一个Fragment的界面文件。

Fragment的界面文件和一般的Activity布局文件一样,如。

<?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="#0000ff"android:orientation="vertical" ><TextView android:layout_width="match_parent"android:layout_height="match_parent"android:text="@string/fragment1"/></LinearLayout>

2、创建第一个Fragment的类文件。

package com.ljh.fragmentdemo;import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;//继续Fragment类。
public class Fragment1 extends Fragment {@Override//在Fragment被加载时调用public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//加载某个布局文件。return inflater.inflate(R.layout.fragment1, null);}
}

3、创建第二个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:background="#00ff00"android:orientation="vertical" ><TextView android:layout_width="match_parent"android:layout_height="match_parent"android:text="@string/fragment2"/></LinearLayout>

4、创建第二个Fragment的类文件。

package com.ljh.fragmentdemo;import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;//继续Fragment类。
public class Fragment2 extends Fragment {@Override//在Fragment被加载时调用public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//加载某个布局文件。return inflater.inflate(R.layout.fragment2, null);}}

5、创建主界面文件。

<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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><fragmentandroid:id="@+id/fragment1"android:name="com.ljh.fragmentdemo.Fragment1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><fragmentandroid:id="@+id/fragment2"android:name="com.ljh.fragmentdemo.Fragment2"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /></LinearLayout>

6、创建主类。

本步骤的关键是用Fragment对象取代布局文件中的内容。

package com.ljh.fragmentdemo;import com.ljh.fragmentdemo.R;import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获得FragmentManager,然后获取FragmentTransaction。FragmentManager fm = getFragmentManager();FragmentTransaction transaction = fm.beginTransaction();//用Fragment动态代替布局文件中的内容transaction.replace(R.id.fragment1, new Fragment1());transaction.replace(R.id.fragment2, new Fragment2());//提交事务transaction.commit();}}

注:

1、对两个Fragment的分别进行编辑,如

 @Override//在Fragment被加载时调用public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//加载某个布局文件。View root =  inflater.inflate(R.layout.fragment1, null);TextView tvContent = (TextView) root.findViewById(R.id.tv_content);tvContent.setText("hello");return root;}

2、其它内容见后面博客:

(1)Android2.3及以前对Fragment的支持。

(2)使用Fragment使不同的尺寸的屏幕展现不同内容。

(3)Activity与Fragment之间的通信。

Fragment之一:Fragment入门相关推荐

  1. android fragment 管理器,Android Fragment 與 Fragment管理器

    Android Fragment 與 Fragment管理器 首先談談Fragement的需求 過去開發人員認為界面之間的跳轉只需要使用多個activity組成就行了: 例如下圖中,在Activity ...

  2. android 标题栏 fragment,切换Fragment 并更换标题栏

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 package com.kingberry.googlemaptracks.adapter; import java.util.ArrayList; im ...

  3. must implement OnFragmentInteractionListener/ Fragment与Activity,Fragment与Fragment之间的信息传递

    出现这个问题的原因, 是Fragment关联的Activity没有实现OnFragmentInteractionListener接口. 那为什么要实现这个接口,以及怎样实现这个接口呢?让我们一步一步来 ...

  4. Fragment嵌套Fragment

    问题1.fragment嵌套fragment不显示问题 通常时候,我们制作底部Tab切换,会用到fragment.即一个Activity下,使用4种fragment.这次遇到的问题是关于fragmen ...

  5. ViewPager+Fragment+ViewPager+Fragment

    最近一段时间,Android行业大不如从前轻松,企业要求越来越高了,就算入职了很多时候Android这块也不太受重视,现在Android开发者还能经常接到新需求就算是很幸运的事了,我最近的工作也是没什 ...

  6. Activity与Fragment,以及Fragment与Fragment之间的数据通讯

    Activity和Fragment无疑是Android开发中使用最多的组件,如果Activity使用了多个Fragment,需要在Activity与Fragment,以及Fragment与Fragme ...

  7. Fragment has not been attached yet Fragment 套 Fragment

    Fragment has not been attached yet Fragment 套 Fragment 在商城项目中使用了 Fragment 套 Fragment的结构,大致框架如下图 Mall ...

  8. android独特fragment,Android Fragment总结

    Android Fragment小结 为何使用Fragment? 实现UI的灵活组建与拆分,与Activity配合可保持Activity的视图不变,转而操作Fragment,就像Activity的模块 ...

  9. Android开发-Fragment嵌套Fragment

    Android开发-Fragment嵌套Fragment 前言 使用依赖 远程仓库地址 布局实现 使用控件 xml代码 Java实现 效果图 项目地址 前言 在大多数公司中,他们会尽量少写Activi ...

  10. 【Android Fragment】Fragment基础

    fragment "分段"."碎片"的意思,一般与Activity一起使用,嵌套在activity中表示为Activity界面的一部分. 它具有自己的生命周期, ...

最新文章

  1. 关于C语言中 字符串常量的问题
  2. 【深度学习】模型训练教程之Focal Loss调参和Dice实现
  3. CSS 基础知识(二)
  4. 代理模式详解(包含原理详解)
  5. 全国计算机等级考试题库二级C操作题100套(第39套)
  6. 有奶瓶的linux系统,用U盘启动BEINI(奶瓶)系统
  7. adobe怎么统计字数_统计数据显示,6 月份桌面 Linux 市场份额攀升至历史新高 | Linux 中国...
  8. ajax 购物车 c#,C#AJAX实例
  9. Python 中的注意点_s2
  10. python维度不一样_numpy数组维度不匹配
  11. 学设计要学python吗_北京学习Python设计大概需要多长时间能学会
  12. 皇帝的新脑-读书笔记
  13. emacs 使用集锦
  14. pod重启策略和状态解释
  15. 计算机毕业设计asp.net的高校科研项目管理系统(源码+系统+mysql数据库+Lw文档)
  16. java中static的用法_Java类中static的用法
  17. 网易帐号通帮你一一列举邮箱注册过的所有网站
  18. 用手机来支付什么-支付的方式
  19. 西部开源学习笔记BOOK3《DNS本地高速缓存服务器》
  20. 【转】很实用的Eclipse小工具——Easy Explore和Explore FS

热门文章

  1. 【详解!思路清晰】1095 解码PAT准考证 (25分)
  2. C语言:L1-031 到底是不是太胖了 (10分)(解题报告)
  3. 【图示】小程序云开发和不使用云开发的区别
  4. pygame 文字输入交互_pygame能接收用户的文本输入吗?
  5. php sdk微信,GitHub - swayer/wechat-php-sdk: 微信公众平台 PHP SDK
  6. Pod资源管理(pod容器分类,k8s添加harbor私库,上传下载私库)
  7. vue mianjs 引用css_vue 学习记录八——webpack中常见的配置项
  8. html效果属性是,htmltransition属性
  9. checksum命令 linux_关于Linux操作系统的一些命令是什么?
  10. java信号灯_java 多线程-信号灯法