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 2459 (后缀数组+RMQ)
题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...
- spring中的BeanFactory与ApplicationContext的作用和区别?
BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...
- .net 接口返回json格式示例
1.新建 InterfaceTestPro1 项目: FILE - New - Project... - Web - ASP.NET Web Forms Application name:Interf ...
- C# 好用的三层架构,项目直接上手用
一.项目结构 注意:1.Common类中的引用添加:右键--添加引用--.NET--选择对应的引用 2.各层之间引用互相添加(这个就不必多说了,三层最基础部分) 3.在添加 Oracle 引用时候 ...
- android 官网处理图片 代码
/** * 获取压缩后的图片 (官网大图片加载对应代码) * * @param res * @param resId * @param reqWidth * 所需图片压缩尺寸最小宽度 * @param ...
- .NET开发工具
1.Two monitors 2.Visual Studio 3.TestDriven.NET 4.JetBrains ReSharper 5.VS样式 https://studiostyl.es/
- 转载--详解tomcat配置
http://www.importnew.com/17124.html 原文链接 几乎所有容器类型的应用都会包含一个名为 server.xml 的文件结构.基本上,其中的每个元数据或者配置都是容器完 ...
- PHP实现分页:文本分页和数字分页
来源:http://www.ido321.com/1086.html 最近,在项目中要用到分页.分页功能是经常使用的一个功能,所以,对其以函数形式进行了封装. // 分页分装 /** * $pageT ...
- 分析特定类的python脚本
今天接触了下pyUSB,事先没看对象内部成员资料,直接用python的dir函数看了看pyUSB的内部构成.突然间想到自己可不可以写个简单的脚本,利用dir或其他函数遍历某个对象内部的所有成员,并打印 ...
- linux 系统常用命令
临时性的完全关闭防火墙,可以不重启机器: #/etc/init.d/iptables status ##查看防火墙状态 #/etc/init.d/iptable stop ...