fragment (1)简单示例:定义,界面配置,fragment之间的跳转
fragment作用
同一程序中切换界面 比activity轻快,灵活.
fragment代码示例
package com.example.f6k5i8.lfragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AnotherFragment extends <span style="color:#33ccff;">Fragment</span> {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//R.layout.fragment_another是本fragment对应的资源文件,container是主布局,
View root = inflater.inflate(R.layout.fragment_another, container, false);
root.findViewById(R.id.btnBackFragment).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().popBackStack();
}
});
return root;
}
}
1.注意 fragment的包
2.界面配置
fragment也有界面布局xml文件: fragment_another.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragmentB_text"
android:id="@+id/anotherFragment"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="后退到上一个Fragment"
android:id="@+id/btnBackFragment"
android:layout_gravity="center_horizontal" />
</LinearLayout>
3.fragment与layout.xml关联
AnotherFragment与它对应的layout.xml关联的代码是 onCreateView函数中的下面语句;
View root = inflater.inflate(R.layout.fragment_another, container, false);
4.跳转到一个新的fragment示例代码
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
getFragmentManager().beginTransaction()
.addToBackStack("AnotherFragment的标记")
.replace(R.id.container, new AnotherFragment())
.commit();
有add,有replace等多种方法.
参数R.id.container是主容器的id,它在activity_main.xml中定义,且不能用R.layout.activity_main.
参数new AnotherFragment() 是被添加的fragment子视图.
注意 addToBackStack 将打开的fragment压进栈.这样方便返回
5.从新fragment返回
前提是本fragment在打开时是addToBackStack里的
getFragmentManager().popBackStack();
fragment (1)简单示例:定义,界面配置,fragment之间的跳转的更多相关文章
- 安卓欢迎界面和activity之间的跳转问题
使用安卓的UI界面,就不得不了解activity,由于actvity就像是一个form表单一样,全部的UI都呈如今这里,他能够承载全部的UI控件. INtent就是一个中继站一样.他负责组件之间的沟通 ...
- ViewPager+Fragment替代TabHost效果的简单示例
本示例旨在展示fragment替代tabhost的效果,具体的业务逻辑还要根据这个示例进行扩展. 效果图如下: 主Activity代码: package com.llb.view; import ja ...
- 【Android】Fragment的简单笔记
被虐了,做某公司笔试时,发现自己连个Fragment的生命周期都写不详细.平时敲代码,有开发工具的便利,有网上各大神的文章,就算忘了也很容易的可以查到,但当要自己不借助外界,却发现自己似乎对该知识点并 ...
- Android开发——Fragment的简单使用总结
前言: 之前搞项目的时候,就使用了这个Fragment,中间遇到了许多坑,把坑都解决了,现在写一篇较为简单的Fragment使用总结 Fragment的简单介绍: 简单来说,Fragment其实可以理 ...
- 【Android】7.0 第7章 简单适配器和布局--本章示例主界面
分类:C#.Android.VS2015: 创建日期:2016-02-09 修改日期:2016-02-13 一.在AssemblyInfo.cs文件中配置应用程序清单 前面的章节我们说过,除了在And ...
- Android流行界面结构——Fragment通过ViewPager(带指示器)嵌套Fragment结构的创建方法详解
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6287213.html 当前Android流行界面结构的一种——Fragment通过ViewPage ...
- Fragment的简单使用
最近一直有点忙,刚刚看到一个朋友的留言提到Fragment中加载ListView的问题,这里写了一个非常简单的测试,至于对Fragment的增.删.改实现动态布局构建灵活的UI,以后有时间在讨论: M ...
- Android App组件之Fragment说明和示例
Android App组件之Fragment说明和示例 1 Fragement介绍 Android从3.0开始引入Fragment,主要是为了支持更动态更灵活的界面设计. Fragment是activ ...
- Android-fragment简介-fragment的简单使用
1.fragment简介 在Android3.0版本之前Google还没有推出fragment,在Android3.0版本之后Google推出了fragment,由于Android3.0版本是过渡期版 ...
随机推荐
- URAL 1161 Stripies(数学+贪心)
Our chemical biologists have invented a new very useful form of life called stripies (in fact, they ...
- Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess
这场CF又掉分了... 这题题意大概就给一个h*w的棋盘,中间有一些黑格子不能走,问只能向右或者向下走的情况下,从左上到右下有多少种方案. 开个sum数组,sum[i]表示走到第i个黑点但是不经过其他 ...
- BETWEEN and
select * from ( select *,ROW_NUMBER() over (ORDER BY AddTime desc) RowNumber from Product where ID n ...
- linux时钟同步
方法1. ntpdate ip 搜索时钟服务器.找一个靠谱的时钟ip执行以上命令即可 可以把这个加入crontab中定时同步.# /usr/sbin/ntpdate 210.72.145.44 > ...
- Oracle Savepoint
1.目的: Use the SAVEPOINT statement to identify a point in a transaction to which you can later roll b ...
- microsoft .netframework Available Source Code Components
http://referencesource.microsoft.com/netframework.aspx http://blogs.msdn.com/b/dotnet/archive/2012/0 ...
- HGE游戏引擎之hgeQuad结构体的使用(用于渲染图片)
HGE基本的渲染图元是hgeQuad (Quad is the basic HGE graphic primitive),其中有一个hgeVertex成员结构,它用来描述图元顶点信息.The hgeV ...
- 鸟哥的linux私房菜学习记录之bash
当你对计算机输入一个指令时,bash会将指令传送给核心kernel,核心再去调用相关的程序,启动硬件. 如果直接让用户操作操作系统,可能会造成系统的崩溃,所以操作系统通过应用程序来让用户操作系统即壳程 ...
- Java排序算法(1)
Java中的排序算法(1) package com.softeem.jbs.lesson4; import java.util.Random; /** * 排序测试类 * * 排序算法的分类如下: ...
- JavaScript,base64加密解密
直接下载吧: http://files.cnblogs.com/files/xiluhua/base64Decode.js