android 77 fragment
fragment是3.0之后才有的,之前平板是3.0专用,后来手机和平板都用3.0
Activity:
package com.itheima.fragment; import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { private Fragment03 fg3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); fg3 = new Fragment03();
//获取fragment管理器
FragmentManager fm = getFragmentManager();
//打开事务
FragmentTransaction ft = fm.beginTransaction();
//把内容显示至帧布局
ft.replace(R.id.fl, fg3);
//提交
ft.commit();
} public void click1(View v){
//把fragment01的界面显示至帧布局中
//创建fragment对象
Fragment01 fg1 = new Fragment01();
//获取fragment管理器
FragmentManager fm = getFragmentManager();
//打开事务
FragmentTransaction ft = fm.beginTransaction();
//把内容显示至帧布局
ft.replace(R.id.fl, fg1);
//提交
ft.commit();
commit()之前没有调用addToBackStack(),那个fragment将会是destroyed;如果调用了addToBackStack(),这个fragment会是stopped,可以通过返回键来恢复。
} public void click2(View v){
//把fragment01的界面显示至帧布局中
//创建fragment对象
Fragment02 fg2 = new Fragment02();
//获取fragment管理器
FragmentManager fm = getFragmentManager();
//打开事务
FragmentTransaction ft = fm.beginTransaction();
//把内容显示至帧布局
ft.replace(R.id.fl, fg2);
//提交
ft.commit();
} public void click3(View v){
//把fragment01的界面显示至帧布局中
//获取fragment管理器
FragmentManager fm = getFragmentManager();
//打开事务
FragmentTransaction ft = fm.beginTransaction();
//把内容显示至帧布局
ft.replace(R.id.fl, fg3);
//提交
ft.commit();
}
}
<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"
tools:context=".MainActivity"
android:orientation="horizontal"
> <FrameLayout
android:id="@+id/fl"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
></FrameLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment01"
android:onClick="click1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment02"
android:onClick="click2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment03"
android:onClick="click3"
/>
</LinearLayout>
</LinearLayout>
fragment1
package com.itheima.fragment; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment01 extends Fragment { //返回的view对象会作为fragment01的内容显示在屏幕上
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//inflater是布局填充器
View v = inflater.inflate(R.layout.fragment01, null);
return v;
} @Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("01create");
} @Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("01start");
} @Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("01resume");
} @Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("01pause");
} @Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("01stop");
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("01destroy");
}
}
<?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:orientation="vertical"
android:background="#ff0000"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热情的红色"
android:textSize="20sp"
/> </LinearLayout>
fragment2
package com.itheima.fragment; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment02 extends Fragment { //返回的view对象会作为fragment02的内容显示在屏幕上
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment02, null);
return v;
}
}
<?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:orientation="vertical"
android:background="#0000ff"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忧桑的蓝色"
android:textSize="20sp"
/> </LinearLayout>
fragment3:
package com.itheima.fragment; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment03 extends Fragment { //返回的view对象会作为fragment03的内容显示在屏幕上
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment03, null);
return v;
} @Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("03create");
} @Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("03start");
} @Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("03resume");
} @Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("03pause");
} @Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("03stop");
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("03destroy");
}
}
<?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:orientation="vertical"
android:background="#00ff00"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小志的绿色"
android:textSize="20sp"
/> </LinearLayout>
android 77 fragment的更多相关文章
- [转]Android:Activity+Fragment及它们之间的数据交换(一)
2014-05-18 来源:Android:Activity+Fragment及它们之间的数据交换(一) 简介: 为什么要用Fragment?使用Fragment可以在一个Acti ...
- Android:Activity+Fragment及它们之间的数据交换.
Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Android中Fragment的两种创建方式
fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...
- android之Fragment基础详解(一)
一.Fragment的设计哲学 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕比手机的大得多,有 ...
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...
- android之fragment的使用
android中的fragment与html中的div很类似,下图中通过左边的按键可以控制右边的显示内容.右边的内容就是一个fragment,通过点击按键来控制fragment的实现. 工程目录 需要 ...
- Android使用Fragment定义弹出数字键盘
fragment主布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...
随机推荐
- 解决Android SDK Manager更新(一个更新Host的程序的原理实现和源码)
<ignore_js_op> 同学遇到了更新Android SDK的问题,而且Goagent现在也无法用来更新.就想到了用替代Host的方法,添加可用的谷歌地址来实现更新. ...
- 网络流系列算法总结(bzoj 3438 1061)
网络流嘛,怎么看都是一堆逗逼题嘛,反正遇到还是都做不起嘛.... 网络流的模板非常简单,难点都在于建图,网络流的建图解决问题范围之广,下至A+B Problem,上至单纯形,线性规划.所以如果对于网络 ...
- 循环冗余校验(CRC)算法入门引导
目录 写给嵌入式程序员的循环冗余校验CRC算法入门引导 前言 从奇偶校验说起 累加和校验 初识 CRC 算法 CRC算法的编程实现 前言 CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌 ...
- C++ Socket TCP "Hello World!"
这是C++ SOCKET网络程序中的C/S结构之TCP "Hello World !",共两个控制台工程: //////////////////////////////////// ...
- Xamarin 开发常见问题
原文:Xamarin 开发常见问题 Verify the project is selected to be deployed in the Solution Configuration Manage ...
- perl 面向对象demo
Vsftp:/root/perl/17# cat Critter.pm package Critter; sub new { my $self = {}; my $invocant = shift; ...
- statspack系列6
原文:http://jonathanlewis.wordpress.com/2006/12/27/analysing-statspack-6/ 作者:Jonathan Lewis 下面是一段时间以前网 ...
- bzoj2186
首先我们看到题目要求的是1~N!内有M!互质的个数 N!>M!,而我们是知道在M!以内与M!互质的数的个数,即phi(M!) 但是M!~N!内与M!互质的数有多少个呢? 对于每个互质的数,如果我 ...
- WordPress RokMicroNews插件‘thumb.php’ 多个安全漏洞
漏洞名称: WordPress RokMicroNews插件‘thumb.php’ 多个安全漏洞 CNNVD编号: CNNVD-201309-384 发布时间: 2013-09-24 更新时间: 20 ...
- 将批量下载的博客导入到手机后,通过豆约翰博客阅读器APP(Android手机)进行浏览,白字黑底,保护眼睛,图文并茂。
首先下面演示的博文来自于以下地址:http://www.douban.com/note/423939291/ 需要先通过博客备份专家将导出的博文导入到手机(还不会用的朋友请先阅读http://www. ...