1、静态有动态

代码如下:

public class MainActivity extends AppCompatActivity {
private ContentFragment cf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setFragment(); }
//动态载入
public void setFragment(){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
cf = new ContentFragment();
ft.replace(R.id.fragment_content,cf);
ft.commit();
}
}

静态载入fragment布局
title_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="@string/title"/> </LinearLayout>

动态载入的布局:
context_layout.xml

<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="@string/context"/>
</LinearLayout>

activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.lesson.hs.fragmentExp.MainActivity">
<!-- 自己定义的fragment-->
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragment_title"
android:name="com.lesson.hs.fragmentExp.TitleFragment"
/>
<!-- 动态载入的fragment容器-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/fragment_title"
android:id="@+id/fragment_content"
/>
</RelativeLayout>

动态载入的fragment类:

public class ContentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.context_layout,container,false);
return view;
}
}

静态载入的fragment类:

public class TitleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.title_layout,container,false);
return view;
}
}

Fragment的2中载入方式!的更多相关文章

  1. 【Android进阶篇】Fragment的两种载入方式

    一.概述 Fragment(碎片,片段)是在Android 3.0后才引入的,基本的目的是为了实如今大屏幕设备上的更加动态更加灵活的UI设计. 这是由于平板电脑的屏幕比手机大得多,所以屏幕上能够放很多 ...

  2. Fragment在Activity中的应用 (转载)

    原文链接 http://www.cnblogs.com/nanxin/archive/2013/01/24/2875341.html 在本小节中介绍在Activity中创建Fragment. 官网有很 ...

  3. FragmentActivity + Fragment + Fragment使用过程中出现的bug

    FragmentActivity + Fragment(通过hide和show来显示fragment) + Fragment(通过viewpager来显示fragment) 在Activity中 // ...

  4. 清空Fragment回退栈中某个Fragment

    出栈的方法:getActivity().getSupportFragmentManager().popBackStackImmediate(CreateOneFragment.class.getNam ...

  5. 解决Fragment在Viepager中切换不保存状态的问题

    在FragmentPagerAdapter中重写以下方法: @Override public Object instantiateItem(ViewGroup container, int posit ...

  6. Fragment在xml中但作用不是显示view

    2013-12-17 有时候会发现在xml文件中有使用fragment,但是却不是为了显示View,代码如下: <FrameLayout xmlns:android="http://s ...

  7. 【原创】【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析   1.使用场景 ViewPager+Fragment实现界面切换,界面数量>=3   2.Fragment生命周期以及与Activ ...

  8. 转载【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析  原文链接 http://www.cnblogs.com/monodin/p/3866441.html 1.使用场景 ViewPager+ ...

  9. Web Fragment在项目中的使用

    Web Fragment 是什么 - 它是在 servlet 3.0开始支持的,可以把一个dy web项目拆分为多个项目,解耦合,使其在项目中开发效率提高,下面我演示简单的项目创建过程 用eclips ...

随机推荐

  1. 微软DbHelper

    using System; using System.Configuration; using System.Data; using System.Data.Common; using System. ...

  2. 模块shimgvw.dll已加载,但找不到入口点DllRegisterServer

    [环境]Windows 7 / Windows Server 2008 [现象]在文件夹浏览器中不能显示图片缩略图. [错误信息]查系统日志,有如下消息:“模块shimgvw.dll已加载,但找不到入 ...

  3. hdu 2089 不要62 数位dp

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. P2312 解方程

    题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...

  5. MUI 版本更新

    MUI版本更新,一些js,css  就不写了. 一.app 端 1.APP html 代码 <li class="mui-table-view-cell"> <a ...

  6. Redis基础

    Redis是开源的,支持网络.可基于内存也可持久化的key-value数据库,支持主从复制,读写分离,支持5种基本数据类型,高并发性能 安装redis数据库 下载:http://redis.io/do ...

  7. C语言培训第一天

    下面是一些命令,先来谈谈今天的若干收获吧! 计算机中的一切文件都是以二进制补码的形式存在,问题也就来了. 第一个问题 如果我们给一个无符号的数赋值一个负数,他会读取到什么,又会输出什么?(似乎问题和上 ...

  8. IIC,RS485,RS232各种协议手册更新中

     RS485使用手册与指南.pdf  RS232协议标准详解.pdf IIC通信协议.pdf 链接:http://pan.baidu.com/s/1ccBtmA 密码:mwj6 IIC,RS485,R ...

  9. CentOS 7 网络配置方法

    [root@zookeeper network-scripts]#  vim /etc/sysconfig/network-scripts/ifcfg-enp0s3 输入以下文本: TYPE=Ethe ...

  10. .Net母版页

    母版页:MasterPage 母版页不是独立的,它是与用户控件一起来使页面结构更加优化. 从粗到细,逐步细化各级页面的模板结构.到页面中只需完善其不同之处即可,相同之处都放在各级母版页中实现了. We ...