Fragments 是android3.0以后添加的。主要是为了方便android平板端的开发。方便适应不同大小的屏幕。此代码是为了最简单的Fragment的使用,往一个Activity中添加Fragment,主要涉及的知识点有:1、Fragment类的创建,2、Fragment的添加3、无UI的 Fragment的添加,根据Tag找回Fragment
Fragment对应的Xml布局文件,
<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"
android:weightSum="10" >
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="添加一个Fragment"
android:layout_weight="2"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="将Fragment加载到Activity中,此Fragment中没有UI,即不需要实现onCreateView方法,可以当做此Activity的背景色" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="根据Fragment的Tag找到Fragment" />
<LinearLayout
android:id="@+id/lv_fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:background="#123456"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
1、Fragment的创建
package com.example.fragment1;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* @author sea
* 创建一个Fragment至少要实现三个生命周期函数onCreate,onCreateView,onPause
*
*/
public class MyFragment extends Fragment {
/*
* 初始化Fragment,实例化在Fragment中的成员变量
*/
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
/*
* 给Fragment 加载UI的布局,返回Fragment布局文件对应的东东
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment,container, false);
return view;
}
/*
* 当用户离开此Fragment时调用
*/
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
2、无UI的Fragment的创建
package com.example.fragment1;
import android.app.Fragment;
import android.os.Bundle;
public class MyFragment2 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
3、Fragment的添加到Activity中
package com.example.fragment1;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author sea
* 将fragment加载到一个Activity中
* 方法一代码:如此例子主要是用到FragmentTransaction类
* 方法二:直接在xml文件中添加
*
*/
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.bt);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//找到FragmentTransaction
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.
beginTransaction();
MyFragment fragment = new MyFragment();
//加到Activity中
fragmentTransaction.add(R.id.lv_fragment_container,fragment);
//加到后台堆栈中,有下一句代码的话,点击返回按钮是退到Activity界面,没有的话,直接退出Activity
//后面的参数是此Fragment的Tag。相当于id
fragmentTransaction.addToBackStack("fragment1");
//记住提交
fragmentTransaction.commit();
}
});
}
}
- Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面
现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 1.跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent ...
- Android开发之Fragment的介绍、使用及生命周期
Fragment官网介绍-http://developer.android.com/guide/components/fragments.html 郭大神的使用实例文章:http://blog.csd ...
- Android开发之Service的写法以及与Activity的通信
Service的总结: 1.按运行地点分类: 类别 区别 优点 缺点 应用 本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外 ...
- Android开发之Fragment
一.Fragment生命周期: 二.动态添加Fragment的三步: 1.获得Fragment的管理者FragmentManager FragmentManager fragmentManager = ...
- Android开发之Fragment传递參数的几种方法
Fragment在Android3.0開始提供,而且在兼容包中也提供了Fragment特性的支持. Fragment的推出让我们编写和管理用户界面更快捷更方便了. 但当我们实例化自己定义Fragmen ...
- Android系列之Fragment(一)----Fragment加载到Activity当中
Android上 的界面展示都是通过Activity实现的,Activity实在是太常用了.但是Activity也有它的局限性,同样的界面在手机上显示可能很好看, 在平板上就未必了,因为平板的屏幕非常 ...
- Android高效异步图片加载框架
概述 Android高效异步图片加载框架:一个高效的异步加载显示的图片加载框架,同时具备图片压缩,缓存机制等特性. 详细 代码下载:http://www.demodashi.com/demo/1214 ...
- Android开发之Activity的生命周期以及加载模式
本篇博客就来好好的搞一下Activity的生命周期,如果搞过iOS的小伙伴的话,Activity的生命周期和iOS中ViewController的生命周期非常类似.生命周期,并不难理解.一个人的生命周 ...
- Android开发之Bitmap的高效加载
BitmapFactory类提供了四类方法:decodeFile, decodeResource, decodeStream和decodeByteArray 分别用于支持从文件系统,资源,输入流以及字 ...
随机推荐
- Dropping water balloons
题意: 给你k个水球n层楼(n很大) 现在做实验在楼上向下丢水球,若水球没破可以重新丢,求把所有水球弄破的最小试验次数. 分析: 开始完全没思路啊.从正面求没法做不会表示状态,做实验是只能从第一层,一 ...
- hdu 1544 水题
水题 /* * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> #i ...
- 《Python 学习手册4th》 第九章 元组、文件及其他
''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...
- Casperjs/PhantomJs 中文网站截图乱码
使用CasperJs进行自动化测试中文网站的时候发现中文网站截图会出现乱码的现象,中文汉字被一个个小方框代替 查找了一些资料发现是因为Linux服务器上没有安装中文字体导致的,Linux如何安装中文字 ...
- 【windows核心编程】DLL相关(3)
DLL重定向 因为DLL的搜索路径有先后次序,假设有这样的场景:App1.exe使用MyDll1.0.dll, App2.exe使用MyDll2.0.dll, MyDll1.0 和 MyDll2.0是 ...
- 解决YUM无法正常工作
1. 错误发生背景 在进行安装依赖包的时候,能够在YUM源中找到相关的RPM包,但是无法进行下载,在单独进行安装RPM包的时候能够进行安装,报错截图如下: 具体的报错信息如下: Error Downl ...
- 使用python的logging模块
一.从一个使用场景开始 开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件 import logging # 创建一个logger logger = logging.getLogger(' ...
- res/raw和assets的 区别
res/raw和assets的相同点: 两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制. res/raw和assets的不同点: 1.res/raw中的文件会被映射到R.ja ...
- bzoj 2438 [中山市选2011]杀人游戏(SCC+概率)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2438 [题意] N个人中有一个杀手,每次询问一个人可能被杀或被告知其认识的人中谁是杀手 ...
- Javascript手记-垃圾收集
如果有人问.net的垃圾回收,大家会马上想到gc,那如果有人问你javascript如何进行内存管理的呢?挠挠头,一口香瓜,听我细细道来! javascript具有自动垃圾收集机制,执行环境会负责管理 ...