activity_m1.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.hanqi.music_bofangqi.MActivity1"> <fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:name="com.hanqi.music_bofangqi.MyFragment1"
android:id="@+id/fragment1"
/>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.hanqi.music_bofangqi.MyFragment2"
android:id="@+id/fragment2"
/>
</LinearLayout>

MActivity.java

package com.hanqi.music_bofangqi;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity; //1-Activity要继承FragmentActivity
public class MActivity1 extends FragmentActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m1);
}
}

activity_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.hanqi.music_bofangqi.FragmentActivity"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加Fragment"
android:onClick="bt1_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="移除Fragment"
android:onClick="bt2_onclick"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/ll"
android:orientation="horizontal"> </LinearLayout> </LinearLayout>

FragmentActivity.java

package com.hanqi.music_bofangqi;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View; public class FragmentActivity extends AppCompatActivity {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
//碎片的容器
// 1.获得放置fragment的布局管理器
//linearLayout= (LinearLayout)findViewById(R.id.ll);
//2.动态加载fragment
//1)得到管理器
fragmentManager =getSupportFragmentManager();
//2) 得到事务管理器
fragmentTransaction = fragmentManager.beginTransaction();
//3)向容器内放入fragment,参数:1-容器id 2-fragment的实例,并提交
fragmentTransaction.add(R.id.ll,new MyFragment1()).commit();
}
MyFragment2 myFragment2; //动态添加
public void bt1_onclick(View v)
{
fragmentManager =getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//加入回退栈
fragmentTransaction.addToBackStack(null);
//3)向容器内放入fragment,参数:1-容器id 2-fragment的实例,并提交
// replace替换
fragmentTransaction.replace(R.id.ll, myFragment2).commit();
}
public void bt2_onclick(View v)
{
fragmentManager =getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//加入回退栈
fragmentTransaction.addToBackStack(null);
//remove移除
fragmentTransaction.remove(myFragment2).commit();
} }

fragment_my_fragment1.xml

<FrameLayout 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"
tools:context="com.hanqi.music_bofangqi.MyFragment1"> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是碎片1"
android:background="#f00"/> </FrameLayout>

fragment_my_fragment2.xml

<FrameLayout 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"
tools:context="com.hanqi.music_bofangqi.MyFragment1"> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是碎片2" /> </FrameLayout>

MyFragment1.java

package com.hanqi.music_bofangqi;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MyFragment1 extends Fragment { public MyFragment1()
{
Log.e("TAG","构造碎片"); }
//在Activity显示完成之后调用的
// 设置Fragment的视图
//返回一个显示的View
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("TAG","onCreateView被调用"); return inflater.inflate(R.layout.fragment_my_fragment1, container, false);
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TAG", "onCreate被调用");
} //在Activity创建完成后调用
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("TAG", "onActivityCreated被调用");
} @Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e("TAG", "onAttach被调用");
} @Override
public void onStart() {
super.onStart();
Log.e("TAG", "onStart被调用");
} @Override
public void onResume() {
super.onResume();
Log.e("TAG", "onResume被调用");
} @Override
public void onPause() {
super.onPause();
Log.e("TAG", "onPause被调用");
} @Override
public void onStop() {
super.onStop();
Log.e("TAG", "onStop被调用");
} @Override
public void onDestroy() {
super.onDestroy();
Log.e("TAG", "onDestroy被调用");
} @Override
public void onDestroyView() {
super.onDestroyView();
Log.e("TAG", "onDestroyView被调用");
} @Override
public void onDetach() {
super.onDetach();
Log.e("TAG", "onDetach被调用");
}
}

MyFragment2.java

package com.hanqi.music_bofangqi;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class MyFragment2 extends Fragment { public MyFragment2()
{ }
//在Activity显示完成之后调用的
// 设置Fragment的视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { //手动创建视图
TextView textView = new TextView(getActivity()); textView.setText("这是手动生成的内容");
return textView;
//return inflater.inflate(R.layout.fragment_my_fragment2, container, false); } }

fragment 碎片整理的更多相关文章

  1. 关闭电脑SSD的磁盘碎片整理

    小白往往会把机械硬盘时代的习惯带进固态硬盘时代,比如碎片整理.机械硬盘时代砖家最喜欢告诉小白:“系统慢了吧?赶紧碎片整理撒.”小白屁颠屁颠地整理去了.殊不知碎片整理对于SSD来说完全就是种折磨.这种“ ...

  2. sql索引碎片产生的原理 解决碎片的办法(sql碎片整理)(转)

    本文讲述了SQL SERVER中碎片产生的原理,内部碎片和外部碎片的概念.以及解决碎片的办法和填充因子.在数据库中,往往每一个对于某一方面性能增加的功能也会伴随着另一方面性能的减弱.系统的学习数据库知 ...

  3. 转载:为什么Linux不需要磁盘碎片整理

    转载自:www.aqee.net 如果你是个Linux用户,你可能听说过不需要去对你的linux文件系统进行磁盘碎片整理.也许你注意到了,在Liunx安装发布包里没有磁盘碎片整理的工具.为什么会这样? ...

  4. 为什么Linux不需要碎片整理?

    如果你是一个 Linux 用户,你可能会听说 Linux 的文件系统不需要碎片整理.你也可能会注意到 Linux 的发行版本也都没有磁盘碎片整理的功能.这是为什么呢? 要理解为什么 Linux 的文件 ...

  5. UltimateDefrag磁盘碎片整理软件 v3.0.100.19汉化版

    软件名称:UltimateDefrag磁盘碎片整理软件 v3.0.100.19汉化版软件类别:汉化软件运行环境:Windows软件语言:简体中文授权方式:免费版软件大小:3.25 MB软件等级:整理时 ...

  6. Defraggler磁盘碎片整理工具,让你的电脑读写速度更快

    相信大家都听说过磁盘碎片整理吧,所谓磁盘碎片,通俗的来说,就是指计算机中的各种文件最开始在磁盘中存储的时候地址都是连在一起的,但是随着文件 的多次读写,或者说多次的移动复制等操作,这些文件在磁盘中的地 ...

  7. 14 Fragment 碎片总结

    Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...

  8. Virtual box中Ubuntu虚拟机磁盘碎片整理和空间清理方法

    虚拟机中,随着不断的使用,增加大文件(例如日志,视频和软件版本),虽然在虚拟机中手动删除了,但是虚拟机占用的空间并不会随之减少,需要手动清理一下. 这里介绍一种Virtual box中Ubuntu碎片 ...

  9. SQL Server索引碎片整理实际操作记录

    SQL Server 版本是 2008 R2. 查询数据库索引碎片情况的 SQL 语句(来源): SELECT OBJECT_NAME(ind.OBJECT_ID) AS TableName, ind ...

随机推荐

  1. IIS-Server is too busy _解决方法

    httpRuntime Server Too Busy 修改方法:修改服务器.net配置“machine.config"文件,该文件位于Windows系统目录下,如“C:\WINDOWS \Micro ...

  2. PHP入门part1

    有人说php是世界上最好的语言,那它好在哪呢. 它是开源自由的软件,能够在所有的操作平台上稳定的运行,入门比较简单.对于我这种没学过什么计算机语言的人是最好的起步点. PHP现在的含义:Hypetex ...

  3. 写MYSQL存储过程遇到的一个小BUG

    DELIMITER $$ USE `income_new`$$ DROP PROCEDURE IF EXISTS `a`$$ CREATE DEFINER=`income_new`@`%` PROCE ...

  4. shell script练习

    执行脚本的几种方式: 1. sh a.sh 或者  bash a.sh  调用的是 /bin/bash 进程执行的,所以脚本不需要执行权限. 2. 直接使用绝对路径执行, /home/script/a ...

  5. oracle两列相同的去重

    源地址:https://zhidao.baidu.com/question/66722841.html 1.不含大字段(clob等)的表格: 1 2 3 4 5 6 7 8 9 --例子表格:crea ...

  6. Linux批量更改文件后缀名

    一.rename解决 1.  Ubuntu系统下 rename 's/\.c/\.h/'  ./* 把当前目录下的后缀名为.c的文件更改为.h的文件 2.  CentOS5.5系统下 rename . ...

  7. ios系统的中arm指令集

    arm结构处理器,几乎所有的手机都基于arm,其在嵌入式系统中应用非常广泛. ARM 处理器因为低功耗和小尺寸而闻名,它的性能在同等功耗的产品中也很出色.这里我们注意一点,模拟器并不运行arm代码,软 ...

  8. 转:Tomcat安装配置及站点说明

    原文地址:http://www.cnblogs.com/Johness/archive/2012/07/20/2600937.html 1.首先是Tomcat的获取和安装. 获取当然得上Apache的 ...

  9. 使用GDB进行调试

    下面是几篇非常好的GDB使用指南: http://www.cs.cmu.edu/~gilpin/tutorial/ http://oss.org.cn/ossdocs/gnu/linux/gdb.ht ...

  10. No.014 Longest Common Prefix

    14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...