#Fragment 是3.0平板才引入进来的,3.0之后就加入了Fragment。原来是一个屏幕就是一个Activity,
>片段,碎片

1. 定义某一个片段的界面 继承Fragment类
public class BlueToothFragment extends Fragment {}
2. 重写Fragment里面的方法
显示Fragment的ui,把布局文件转化成view对象
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.blue_tooth_layout, null);
}
3. 获取Fragment管理器
fm = getFragmentManager();
4. 动态的修改界面
f1 = new BlueToothFragment();//先把要显示的f1new出来
FragmentTransaction ft = fm.beginTransaction();//开启事务
ft.replace(R.id.fl_container, f1);
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"
android:orientation="vertical" 垂直排列
tools:context=".MainActivity" > <FrameLayout
android:id="@+id/fl_container"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="0dip" >
</FrameLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#22000000"
android:orientation="horizontal" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setting01"
android:text="蓝牙设置" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setting02"
android:text="声音设置" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setting03"
android:text="显示设置" />
</LinearLayout> </LinearLayout>

3个fragment:

蓝牙fragment:

<?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" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是蓝牙设置"
android:textAppearance="?android:attr/textAppearanceLarge" /> <RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" /> <ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" /> </LinearLayout> 显示fragment: <?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" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是显示设置"
android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" /> <RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" /> </LinearLayout> 声音fragment: <?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" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是声音设置"
android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" /> </LinearLayout>

主Activity:

package com.itheima.fragementdemo;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View; public class MainActivity extends Activity {
private FragmentManager fm;//管理所有Fragment,更新页面的时候要么同时更新成功要么同时更新失败所以有事物,
BlueToothFragment f1;
ShowFragment f3;
SoundFragment f2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm = getFragmentManager();
initFragment();
//事务的概念
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fl_container, f1);
ft.commit();//保证了 要么同时成功,要么同时失败
} private void initFragment() {
f1 = new BlueToothFragment();//Fragment是直接new出来的
f2 = new SoundFragment();
f3 = new ShowFragment();
} //蓝牙点击事件
public void setting01(View view) {
//事物开始的时候实例化ft,事物结束的时候提交事物,提交后事物就过期了,所以每次都要初始化ft
FragmentTransaction ft = fm.beginTransaction();//
ft.replace(R.id.fl_container, f1);
ft.commit();//保证了 要么同时成功,要么同时失败,保证了页面不花屏。
}
//声音点击事件
public void setting02(View view) {
//事务的概念
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fl_container, f2);
ft.commit();//保证了 要么同时成功,要么同时失败
}
//显示点击事件
public void setting03(View view) {
//事务的概念
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fl_container, f3);
ft.commit();//保证了 要么同时成功,要么同时失败
} }

3个fragment java代码

蓝牙:

package com.itheima.fragementdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class BlueToothFragment extends Fragment {
//显示Fragment的ui的,不需要在清单文件配置任何内容
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.blue_tooth_layout, null);
}
} 声音 package com.itheima.fragementdemo; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class SoundFragment extends Fragment {
//显示Fragment的ui的
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.sound_layout, null);
}
} 显示 package com.itheima.fragementdemo; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class ShowFragment extends Fragment {
//显示Fragment的ui的
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.show_layout, null);
}
}

##Fragment的向下兼容,兼容低版本
>使用support-v4的jar包
>1. MainActivity extends FragmentActivity
>2. 所有的fragment的导包, android.support.v4.app.Fragment
>3. getSupportFragmentManager();
>4. 注意两个导包
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

package com.itheima.fragementdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View; public class MainActivity extends FragmentActivity {
private FragmentManager fm;
BlueToothFragment f1;
ShowFragment f3;
SoundFragment f2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//为了向下版本兼容采用v4包里面的FragmentManager
fm = getSupportFragmentManager();
initFragment();
//事务的概念
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fl_container, f1);
ft.commit();//保证了 要么同时成功,要么同时失败
} private void initFragment() {
f1 = new BlueToothFragment();
f2 = new SoundFragment();
f3 = new ShowFragment();
} //蓝牙
public void setting01(View view) {
//事务的概念
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fl_container, f1);
ft.commit();//保证了 要么同时成功,要么同时失败
}
//声音
public void setting02(View view) {
//事务的概念
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fl_container, f2);
ft.commit();//保证了 要么同时成功,要么同时失败
}
//显示
public void setting03(View view) {
//事务的概念
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fl_container, f3);
ft.commit();//保证了 要么同时成功,要么同时失败
} }

实现效果:

android 63 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. bzoj 3153: Sone1 Toptree

    3153: Sone1 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 511  Solved: 202[Submit][Status][Discuss ...

  2. BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏

    Description Farmer John 想让她的奶牛准备郡级跳跃比赛,贝茜和她的伙伴们正在练习跨栏.她们很累,所以她们想消耗最少的能量来跨栏. 显然,对于一头奶牛跳过几个矮栏是很容易的,但是高 ...

  3. 【技术贴】解决Program Files文件夹消失

    好久不写程序了,今天良心发现,就寻找一下自己是否安装了JDK,习惯性的去C盘的Program Files的文件夹下面去找,次奥,没有这个文件夹.好吧.是在玩我么. 于是 打开cmd 输入如下命令 AT ...

  4. 反射实体自动生成EasyUi DataGrid模板

    用EasyUi Datagrid展示数据的时候总是要一下这样一段代码 <table id="dt" class="easyui-datagrid"> ...

  5. iOS,object-c传参c语言的二维数组

    有那么一瞬间,懒得用NSArray,NSNumber,NSValue等一大堆蛋疼的转换,所以就定义了一个C的二维数组,反正OC支持C混编,可是蛋疼往往是传递的,这里不疼了,哪里就要疼,想把一个c的二维 ...

  6. SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean

    1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...

  7. IBinder类的中文翻译

    远程对象的基础接口,是一个为了在执行进程中和进程间调用时的高性能,而设计的轻量级远程调用机制的核心部分.这个接口描述了和远程对象交互的抽象协议.不要直接实现这个接口,而是通过继承Binder来实现. ...

  8. hadoop2.2编程:mapreduce编程之二次排序

    mr自带的例子中的源码SecondarySort,我重新写了一下,基本没变. 这个例子中定义的map和reduce如下,关键是它对输入输出类型的定义:(java泛型编程) public static ...

  9. wifi测试相关(iwconfig,WPA Supplicant用法)

    iwconfig用法 1.打开无线网卡电源 iwconfig wlan0 txpower no 2.列出区域内的无线网络 iwconfig wlan0 scan 3.假设要连接到网络myhome(即e ...

  10. Matlab无法打开M文件的错误( Undefined function or method 'uiopen' for input arguments of type 'char)

    错误提示: Undefined function or method 'uiopen' for input arguments of type'char 解决方案: 运行命令 restoredefau ...