Android Fragment(碎片)的使用
- 简介
在Android中Fragment为一种可以嵌入活动中的UI片段.能让程序更加合理地利用大屏幕的空间. - 使用方法
1.我们首先新建的一个onefragment.xml文件.
<?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:background="#e3e3e3"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_right_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="this is fragment"
/>
</LinearLayout>
2.新建一个OneFragment.class(这里要注意的是,不要继承V4包)
public class OneFrament extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.another_right_fragment, container, false);
return view;
}
}
3.使用OneFrament
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmentdemo.OneFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
- 动态添加Fragment.
1.在要添加到的activity.xml布局里面添加.
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" >
</FrameLayout>
2.在要添加到的activity.class里添加.
AnotherRightFrament frament=new AnotherRightFrament(); //实例化要添加的Fragment
FragmentManager fragmentManager=getFragmentManager(); //取到FragmentManager.
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, frament); //根据ID指定要添加到的FrameLayout
transaction.commit();
- 在上面.
1.创建待添加的碎片实例.
2.获取到FragmentManager,在活动中可以直接调用getFramentManager()方法得到.
3.开启一个事务,通过调用beginTransaction()方法开启.
4.向容器内添加碎片,一般使用replace()方法,需要传入容器的ID和待添加的碎片实例.
5.提交事务,调用commit()方法来完成.
更......
- 当我们运行上面的程式,跳转到另一个fragment的时候,习惯性的按下back返回,当却发现程序退出了.这个时候我们就要在碎片中模拟返回栈.
transaction.addToBackStack(null);
AnotherRightFrament frament=new AnotherRightFrament();
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, frament);
transaction.addToBackStack(null);
transaction.commit();
我们在事务提交之前调用了FragmentTransaction的addToBackStack()方法,他可以接受一个名字用于描述返回栈的状态,一般传入null就可以了.
- 碎片和活动之间进行通信
getFragmentManager().findFragmentById(R.id.one_fragment); //android提供了这样一种方式,一个类似于findviewbyid()的方法,专门来从布局文件获取fragment的实例.
MainActivity activity=(MainActivity) getActivity(); //android 提供了getactivity()来得到和当前碎片有关的activity实例.
Android Fragment(碎片)的使用的更多相关文章
- Android Fragment碎片
什么是碎片? 碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛.可以把Fragment当成Activity一个界面的一 ...
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...
- Android Fragment完全解析,关于碎片你所需知道的一切
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...
- Android Fragment完全解析,关于碎片你所需知道的一切 (转)。
我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activity也有它的局限性,同样的界面在手机 ...
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml(转)
注明:这个转的,见谅未能标明原始出处 我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activ ...
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml
Android开发:碎片Fragment完全解析 为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...
- (转) Android Fragment完全解析,关于碎片你所需知道的一切
我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activity也有它的局限性,同样的界面在手机 ...
- Android Fragment应用实战
现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...
- Android Fragment
1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...
随机推荐
- mac下 WebStorm下主题包安装
mac下: 主题包 1.mac下,点击桌面,使用shift+command+G 输入:~/Library/Preferences 前往(mac查找安装目录的方法,因为默认这些文件夹是隐藏的),进入We ...
- Mysql 配置慢查询日志(SlowQueryLog)以及使用日志分析工具
[ 查看系统关于慢查询的设置 ] mysql> show variables like '%slow%'; +---------------------------+-------------- ...
- log4net使用(包括单个文件和按日期生成多个文件)
1.log4net生成单个文件 直接将这段代码考到config中即可用 <log4net> <!--定义输出到文件中--> <appender name="Lo ...
- windows对象的属性和方法
window对象是所有客户端javascript特性和API的主要接入点.它表示web浏览器的一个窗口或窗体,并且可以用标志符window来引用它. 一. windows对象的属性 1. locati ...
- 英文Ubuntu下Emacs 使用 ibus 五笔
Ubuntu 12.04 Precise 英文版本 1. 安装ibus/安装ibus-table-wubi http://wiki.ubuntu.org.cn/IBus 其中im-switch命令要在 ...
- 关于SQL SERVER 2008 R2发布订阅功能
实现功能:主服务器维护数据的变更,从服务器需要时同步主服务器的数据. 配置:主从服务器均为SQL SERVER 2008 R2. 实现方法: 预备: 1.主从服务器需建立同一账户名及密码的账户(当时我 ...
- mysql null值转换
1.如果为空返回0 select ifnull(null,0) 2.如果为空返回0,否则返回1 select if(isnull(col),0,1) as col.
- 非常棒的Java REST服务器栈
Dropwizard 是一个开源的Java框架,用于开发OPS友好.高性能的基于REST的后端.它是由Yammer开发的,来驱动基于JVM的后端. Dropwizard提供同类最佳的Java库到一个嵌 ...
- OpenCV视屏跟踪
#include <stdio.h> #include <iostream> #include "opencv2/imgproc/imgproc.hpp" ...
- poj 3370 Halloween treats(鸽巢原理)
Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...