Android学习备忘笺02Fragment
Android中Fragment可以将UI界面分成多个区块,一般静态或动态添加Fragment。
01.新建Fragment实例
一个Fragment实例包括两个部分:类对象和布局文件(可视化部分)。
类对象继承Fragment,重写onCreateView方法,而布局文件作为View实例参数传入。
a.新建一个布局文件:frag.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:text="Look! What a beautiful fragment."
android:textSize="40sp"
android:textStyle="italic"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:src="@mipmap/orange"
android:layout_width="wrap_content"
android:layout_height="300dp" /> </LinearLayout>
b.新建一个类:fragmentMain.java
public class fragmentMain extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//必须重写onCreateView();
//将Fragment的布局文件转成View实例
View view= inflater.inflate(R.layout.frag,null);
return view;
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
02.静态添加
静态添是在布局文件activity_main.xml 中声明<fragment></fragment>标签,name属性来指定需要添加的Fragment类实例。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:textSize="40sp"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment Demo"
/> <fragment
android:id="@+id/fra"
android:name="com.xxxx.xxxx.xxxx.fragmentMain"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>
03.动态添加
动态添加指通过代码添加,而不是在布局文件中声明<fragment></fragment>标签,但是此时需要一个容器装载Fragment实例。
例如如下:activity_main.xml 将LiinearLayout作为容器,此时应为其添加id。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:textSize="40sp"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment Demo"
/>
<LinearLayout
android:id="@+id/fragment"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </LinearLayout>
</LinearLayout>
然后通过代码添加实例。
//获取FragmentManager实例
FragmentManager fragmentManager=getFragmentManager();
//开始事务
FragmentTransaction trans= fragmentManager.beginTransaction();
//替换在Fragment容器中的Fragment实例
trans.replace(R.id.fragment,new fragmentMain());
//提交
trans.commit();
!注意
有时导入不是 android.app.FragmentManager;包而是 android.support.v4.app.Fragment;时,如果根据是否向下兼容的情况替换。
如果是android.support.v4.app.Fragment;包,应该使用 getSupportFragmentManager();获取实例。
未添加Fragment与添加Fragment的效果如下:
未添加:

添加:

暂时结束咯。
Android学习备忘笺02Fragment的更多相关文章
- Android学习备忘笺01Activity
01.设置视图 在Android Studio新建的项目中,通过 setContentView(R.layout.activity_main);方法将res/layout/activity_main. ...
- leaflet 学习备忘
leaflet 开源js地图工具.非常好用. leaflet参考:http://leafletjs.com/ 特性: 完全开源,可以基于不同的第三方瓦片生成地图. 基于原始GPS,无需转换 可创建离线 ...
- UML学习备忘
两大类UML图: 行为图(behavior diagrams)和结构图(structure diagrams) 行为图将引导系统分析员分析且理清"系统该做些什么"?系统分析 ...
- Git学习备忘
本文参考廖雪峰写的精彩的git学习文档,大家可以直接去官网看原版,我这里只是便于自己记录梳理 原版地址:http://www.liaoxuefeng.com/wiki/0013739516305929 ...
- MVC4 学习备忘
WebConfig文件里添加数据库链接字符: <add name="MovieDBContext(可以自己取字符串名字)" connectionString="Da ...
- 一滴一点vim(学习+备忘)
普通模式: h j k l 分别是左下上右方式移动: :w 保存修改 :q 推出 :wq 保存修改并退出 :q! 放弃修改强制推出 x 删除光标所在位置字符 i 在光标所以位置插入字符 删除类命令: ...
- Android系统备忘1
Android的4种模式 模式 功能 ADB调试system 正常使用 开发者模式开启usb调试recovery 备份,恢复模式 卡刷模式 twrp下开启ADB Sideloadfastboot 线刷 ...
- selenium学习备忘
在做web项目的自动化端到端测试时主要使用的是Selenium WebDriver来驱动浏览器.Selenium WebDriver的优点是支持的语言多,支持的浏览器多.主流的浏览器Chrome.Fi ...
- xml 基础学习备忘
<?xml version="1.0" encoding="UTF-8"? standalone="yes"> 这里的encod ...
随机推荐
- Python的字符串和列表和字典的方法/函数
字符串 S.find()#可指定范围查找字串,返回索引值,否则返回-1 S.index()#同find,只是找不到的之后返回异常 S.count()#返回找到字串的个数 S.lower()#转小写 S ...
- LoadRunner中存储表格参数------关联数组
主要用到 web_reg_save_param_ex函数("Scope=All",), sprintf( CProdNo,"{CProdNo_%d}",i ); ...
- 浅谈c#的三个高级参数ref out 和Params C#中is与as的区别分析 “登陆”与“登录”有何区别 经典SQL语句大全(绝对的经典)
浅谈c#的三个高级参数ref out 和Params c#的三个高级参数ref out 和Params 前言:在我们学习c#基础的时候,我们会学习到c#的三个高级的参数,分别是out .ref 和 ...
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Information ...
- ReLu(修正线性单元)、sigmoid和tahh的比较
不多说,直接上干货! 最近,在看论文,提及到这个修正线性单元(Rectified linear unit,ReLU). Deep Sparse Rectifier Neural Networks Re ...
- jQuery操作Form表单元素
Web开发中常常须要操作表单,form表单元素有select.checkbox.radio.textarea.button.file.text.hidden.password等. 当中checkbox ...
- Java面试通关要点汇总整理【终极版】(转载)
简历篇 请自我介绍 请介绍项目 基础篇 基本功 面向对象的特征 final, finally, finalize 的区别 int 和 Integer 有什么区别 重载和重写的区别 抽象类和接口有什么区 ...
- Linux学习笔记:系统启动引导过程
Linux系统启动引导过程 近期发现自己在仅仅是掌握上有几个比較硬的伤: 一.知识体系碎片,比方Linux,这学点那学点,结果没有成体系,串不起来: 二.记忆时间短暂,非常多的内容学了就忘,最后的结果 ...
- FFT做题记录
FFT是用来快速求卷积的..... 那么卷积有什么作用呢 https://www.zhihu.com/question/22298352 看完就懂了
- Qt 学习之路 2(19):事件的接受与忽略(当重写事件回调函数时,时刻注意是否需要通过调用父类的同名函数来确保原有实现仍能进行!有好几个例子。为什么要这么做?而不是自己去手动调用这两个函数呢?因为我们无法确认父类中的这个处理函数有没有额外的操作)
版本: 2012-09-29 2013-04-23 更新有关accept()和ignore()函数的相关内容. 2013-12-02 增加有关accept()和ignore()函数的示例. 上一章我们 ...