fragment 碎片整理

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 碎片整理的更多相关文章
- 关闭电脑SSD的磁盘碎片整理
小白往往会把机械硬盘时代的习惯带进固态硬盘时代,比如碎片整理.机械硬盘时代砖家最喜欢告诉小白:“系统慢了吧?赶紧碎片整理撒.”小白屁颠屁颠地整理去了.殊不知碎片整理对于SSD来说完全就是种折磨.这种“ ...
- sql索引碎片产生的原理 解决碎片的办法(sql碎片整理)(转)
本文讲述了SQL SERVER中碎片产生的原理,内部碎片和外部碎片的概念.以及解决碎片的办法和填充因子.在数据库中,往往每一个对于某一方面性能增加的功能也会伴随着另一方面性能的减弱.系统的学习数据库知 ...
- 转载:为什么Linux不需要磁盘碎片整理
转载自:www.aqee.net 如果你是个Linux用户,你可能听说过不需要去对你的linux文件系统进行磁盘碎片整理.也许你注意到了,在Liunx安装发布包里没有磁盘碎片整理的工具.为什么会这样? ...
- 为什么Linux不需要碎片整理?
如果你是一个 Linux 用户,你可能会听说 Linux 的文件系统不需要碎片整理.你也可能会注意到 Linux 的发行版本也都没有磁盘碎片整理的功能.这是为什么呢? 要理解为什么 Linux 的文件 ...
- UltimateDefrag磁盘碎片整理软件 v3.0.100.19汉化版
软件名称:UltimateDefrag磁盘碎片整理软件 v3.0.100.19汉化版软件类别:汉化软件运行环境:Windows软件语言:简体中文授权方式:免费版软件大小:3.25 MB软件等级:整理时 ...
- Defraggler磁盘碎片整理工具,让你的电脑读写速度更快
相信大家都听说过磁盘碎片整理吧,所谓磁盘碎片,通俗的来说,就是指计算机中的各种文件最开始在磁盘中存储的时候地址都是连在一起的,但是随着文件 的多次读写,或者说多次的移动复制等操作,这些文件在磁盘中的地 ...
- 14 Fragment 碎片总结
Fragment 碎片 一, Fragment是什么? Android 3.0以后出现的 Api11 以上 Activity的组成部分 Fragment(小的Activity) Fragment可以显 ...
- Virtual box中Ubuntu虚拟机磁盘碎片整理和空间清理方法
虚拟机中,随着不断的使用,增加大文件(例如日志,视频和软件版本),虽然在虚拟机中手动删除了,但是虚拟机占用的空间并不会随之减少,需要手动清理一下. 这里介绍一种Virtual box中Ubuntu碎片 ...
- SQL Server索引碎片整理实际操作记录
SQL Server 版本是 2008 R2. 查询数据库索引碎片情况的 SQL 语句(来源): SELECT OBJECT_NAME(ind.OBJECT_ID) AS TableName, ind ...
随机推荐
- knockout 学习实例4 css
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- ef执行记录原生态代码方法。
select e; var f = (System.Data.Objects.ObjectQuery<SimpleEntry>)final; var s = f.ToTraceString ...
- Android AChartEngine 个性化设置
AChartEngine的确是一个强大的图标引擎,但文档写得不是很详细,很多设置只能通过方法名推测和实际尝试,下面是一些自己在实际中遇到的需要设置的选项,常见的那些和通过方法名就能轻松猜到的就不赘述了 ...
- python学习-day14:集合,函数,格式化
一.集合 定义:由不同元素组成的集合.集合是一组无序排列的可hash值, 可以作为字典的key.元素必须是不可变类型:只能存放数字,字符串,字典 特性:集合的目的是将不同的值放在一起,不同的集合之间可 ...
- sublime3中文乱码解决包ConvertToUTF8.zip
把ConvertToUTF8.zip解压放到C:\Program Files\Sublime Text 3\Data\Packages中,重启sublime 3,按ctrl+shift+c即可解决中文 ...
- Web Api 上传图片,解决上传图片无格式
制作这个功能时,找了很多资料,不过忘记了地址,所以就不一一放连接了, 直接上代码吧! 1. 首先新建一个上传的控制器 /// <summary> /// 上传 /// </summa ...
- java中Date与String的相互转化
1:大体思路 这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new Date("2008-04 ...
- .net 中生成二维码的组件
http://qrcodenet.codeplex.com/
- 黄聪:C#操作Word表格的常见操作(转)
几种常见C#操作Word表格操作有哪些呢?让我们来看看具体的实例演示: bool saveChange = false; //C#操作Word表格操作 object missing = System. ...
- 安卓:assets目录下的文本文件(不受R文件节制)
try { InputStream in = getAssets().open("testAsset.txt"); byte[] buffer = new byte[1024]; ...