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的更多相关文章

  1. [转]Android:Activity+Fragment及它们之间的数据交换(一)

    2014-05-18         来源:Android:Activity+Fragment及它们之间的数据交换(一)   简介: 为什么要用Fragment?使用Fragment可以在一个Acti ...

  2. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  3. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  4. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  5. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  6. android之Fragment基础详解(一)

      一.Fragment的设计哲学 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕比手机的大得多,有 ...

  7. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

  8. android之fragment的使用

    android中的fragment与html中的div很类似,下图中通过左边的按键可以控制右边的显示内容.右边的内容就是一个fragment,通过点击按键来控制fragment的实现. 工程目录 需要 ...

  9. Android使用Fragment定义弹出数字键盘

    fragment主布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...

随机推荐

  1. 10 个 jQuery 的无限滚动的插件:

    很多社交网站都使用了一些新技术来提高用户体验,而无限滚动的翻页技术就是其中一项,当你页面滑到列表底部时候无需点击就自动加载更多的内容. 下面为你推荐 10 个 jQuery 的无限滚动的插件: 1.  ...

  2. Stanford CoreNLP--Named Entities Recognizer(NER)

    Standford Named Entities Recognizer(NER),命名实体识别是信息提取(Information Extraction)的一个子任务,它把文字的原子元素(Atomic ...

  3. just so you're clear

    The Google Resume的第一句话是: Just so you're clear: it was not my idea to give a talk to Microsoft Resear ...

  4. minicom-2.4安装配置

    minicom-2.4安装说明 1.#tar –zxvf minicom-2.4.tar.gz 解压开有连个文件,minicom-2[1].4.tar.gz  和minirc.dfl rpm包方式# ...

  5. statspack系列4

    原文:http://jonathanlewis.wordpress.com/2006/12/27/analysing-statspack-4/ 作者:Jonathan Lewis 使用statspac ...

  6. BZOJ1831: [AHOI2008]逆序对

    1831: [AHOI2008]逆序对 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 341  Solved: 226[Submit][Status] ...

  7. android Spinner的使用

    首先是MainActivity package com.example.spinnertest; import java.util.ArrayList; import java.util.List; ...

  8. win7怎么调屏幕自动休眠时间

    win7怎么调屏幕自动休眠时间 2013-03-28 17:13匿名 | 分类:Windows | 浏览1327次 我也不知道怎么说 我的电脑的问题就是 电脑放那不动过2,3分钟屏幕就暗了 要是不动过 ...

  9. 深入学习JS: __doPostBack函数

    在.NET中,所有的服务器控件提交到服务器的时候,都会调用__doPostBack这个函数,所以灵活运用这个函数对于我们的帮助还是很大的. 比如,在我们写程序的时候经常会需要动态的生成一些控件,最简单 ...

  10. 初次运行 Git 前的配置

    初次运行 Git 前的配置 一般在新的系统上,我们都需要先配置下自己的 Git 工作环境.配置工作只需一次,以后升级时还会沿用现在的配置.当然,如果需要,你随时可以用相同的命令修改已有的配置. Git ...