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 分别用于支持从文件系统,资源,输入流以及字 ...
随机推荐
- Android 翻页效果 电子书
转载请注明来自: 5进制空间-android区 相信做电子书的同学,都遇到过翻页动画的需求吧,如果你不满足与点击滑动翻页的话,这边文章应该能够帮助到你. 先上个效果图: 效果还是很不错的,不过与ibo ...
- codeforces 691D Swaps in Permutation DFS
这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...
- FOJ 1962 新击鼓传花游戏 线段树
维护一个sum数组,有点划分树的思想,写过划分树的应该能看出来 #include<cstdio> #include<algorithm> #include<iostrea ...
- [Tommas] 一种有效的测试策略(转)
在最近的一个大型项目中,我们在早期就定下了一个目标:不会在软件中使用大量QA人员专注于手工测试.通过手工测试发现bug极其耗时且成本高昂,这促使团队尝试尽可能的将质量内嵌到产品内部.但这并不意味着手工 ...
- JAVA与图形界面开发(Applet应用程序、AWT库、Swing)
Applet 1)简单说,Applet就是嵌入到网页中的小程序,Java代码. 2)编写Applet程序,要继承JApplet类,并根据自己需要覆写相关方法(init.start.stop.destr ...
- OpenJDK与JDK的区别及Ubuntu下的安装方法
OpenJDK与JDK的区别: OpenJDK是JDK的开放原始码版本,以GPL协议的形式放出.两者的授权协议的不同,且在采用GPL协议的OpenJDK中,SUNJDK的一部分源代码因为产权的问题无法 ...
- ArcMap10.1无法保存编辑的内容
问题描述:在arcMap10.1中编辑SDE库中要素,保存编辑内容时报错: 无法保存编辑内容.基础DBMS错误[ORA-29877:failed in the execution of the ODC ...
- Genesis-3D开源游戏引擎简介!
Genesis-3D由搜狐畅游公司超百人引擎研发团队历时数年耗费巨资自主研发,是国内外首款商业开源的3D游戏引擎平台.它包括跨平台渲染引擎.2D引擎.物理引擎.音效系统.粒子系统.动画系统.服务器引擎 ...
- sys.check_constraints
每个用作 CHECK 约束(sys.objects.type = C)的对象都在表中占一行. SELECT name FROM sys.check_constraints -- equal to SE ...
- markdown常用html标签
换行 测试<br />一下 效果:测试一下 标记 <mark>测试一下</mark> 效果:测试一下 空格 测试一下 效果: 测试一下 ...