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 分别用于支持从文件系统,资源,输入流以及字 ...
随机推荐
- HDU 4267 A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- [Everyday Mathematics]20150107
设 $f\in C^1[a,b]$, $f(a)=0$, 且存在 $\lm>0$, 使得 $$\bex |f'(x)|\leq \lm |f(x)|,\quad \forall\ x\in [a ...
- SQL Server缺省约束、列约束和表约束
SQL Server缺省约束是SQL Server数据库中的一种约束,下面就为您介绍SQL Server缺省约束.列约束和表约束的定义方法啊,供您参考. SQL Server缺省约束 SQL Serv ...
- asp.net MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别
@Html.Action:需要有对应的Action,并且Action方法有返回值.(注:处理完业务逻辑同时,也需要返回所需值) @{Html.RenderAction}:需要有对应的Action,Ac ...
- XNA Game Studio 4.0 Programming 随便读,随便记 “Game Class”
XNA 中的 Game 类,是所有神奇事情发生的地方.几乎游戏中所有的事情都由它来操办. 它是项目中的王者,让我们深入窥探一番: 虚方法 Game 本身从众多其它地方继续了许多能力才能完成游戏中的事情 ...
- CSS_网站配色参考方案
http://www.cnblogs.com/QLeelulu/archive/2008/04/04/1136974.html Shiny silver [#EEEEEE] Reddi ...
- HDFS体系结构
HDFS的设计目标 检测以及快速恢复硬件问题. 流式的数据访问. 移动计算比移动数据的代价小. 简化一致性模型. 超大规模数据集 异构软硬件平台之间的可移植性. HDFS的结构模型HDFS是一个主从的 ...
- 转:高性能Mysql主从架构的复制原理及配置详解
温习<高性能MySQL>的复制篇. 1 复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台 ...
- COS回应7大质疑
Apple过于封闭,没啥朋友,这家伙应该比较高傲,曾和Intel,IBM and so on..一起玩过!Google过于开放,狐朋狗友,友人泛滥,殃及ecosystem,弊端已显,祸水将至.COS奉 ...
- 第二百四十四、五天 how can I 坚持
昨天忘了.不知咋忘的,加班加迷糊了? 昨天联调接口,又加班了,好歹基本调通了. 今天,下午,开会,有点被领导批的意思,不是批我,是批我们团队. 团队. 不懂自己. 这样做有意义嘛. 睡觉.好烦. 到底 ...