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的更多相关文章

  1. Android学习备忘笺01Activity

    01.设置视图 在Android Studio新建的项目中,通过 setContentView(R.layout.activity_main);方法将res/layout/activity_main. ...

  2. leaflet 学习备忘

    leaflet 开源js地图工具.非常好用. leaflet参考:http://leafletjs.com/ 特性: 完全开源,可以基于不同的第三方瓦片生成地图. 基于原始GPS,无需转换 可创建离线 ...

  3. UML学习备忘

    两大类UML图: 行为图(behavior diagrams)和结构图(structure diagrams)     行为图将引导系统分析员分析且理清"系统该做些什么"?系统分析 ...

  4. Git学习备忘

    本文参考廖雪峰写的精彩的git学习文档,大家可以直接去官网看原版,我这里只是便于自己记录梳理 原版地址:http://www.liaoxuefeng.com/wiki/0013739516305929 ...

  5. MVC4 学习备忘

    WebConfig文件里添加数据库链接字符: <add name="MovieDBContext(可以自己取字符串名字)" connectionString="Da ...

  6. 一滴一点vim(学习+备忘)

    普通模式: h j k l 分别是左下上右方式移动: :w 保存修改 :q 推出 :wq 保存修改并退出 :q! 放弃修改强制推出 x 删除光标所在位置字符 i 在光标所以位置插入字符 删除类命令: ...

  7. Android系统备忘1

    Android的4种模式 模式 功能 ADB调试system 正常使用 开发者模式开启usb调试recovery 备份,恢复模式 卡刷模式 twrp下开启ADB Sideloadfastboot 线刷 ...

  8. selenium学习备忘

    在做web项目的自动化端到端测试时主要使用的是Selenium WebDriver来驱动浏览器.Selenium WebDriver的优点是支持的语言多,支持的浏览器多.主流的浏览器Chrome.Fi ...

  9. xml 基础学习备忘

    <?xml version="1.0" encoding="UTF-8"? standalone="yes"> 这里的encod ...

随机推荐

  1. enumerateObjectsUsingBlock 、for 、for(... in ...) 的差别 &amp; 性能測试

    for VS for(... in ...) for 的应用范围广基本能够NSArray.NSArray以及C语言的数组等,而for(... in ...)仅限于NSArray.NSArray等 fo ...

  2. Servlet的引入

    一.分析 此模式有问题: 1.jsp需要呼叫javabean StudentService stuService = new StudentServiceImpl(); List<Student ...

  3. windows下Python扩展问题error: Unable to find vcvarsall.bat

    由于对于Windows下Python扩展不熟,今天遇到一个安装问题,特此做个tag.解决方式在stackoverflow上,网址例如以下: http://stackoverflow.com/quest ...

  4. putty字体大小颜色、全屏/退出全屏快捷键 保存session设置[转]

    字体大小设置 Window->Appearance->Font settings->Change按钮设置(我的设置为16)字体为(Consolas) 字体颜色设置 Window-&g ...

  5. C项目实践--图书管理系统(3)

    接下来将要实现用户管理模块的相关功能,用户管理模块的主要功能包括增加用户,查找用户以及保存用户等功能,查找用户时,如果查找成功,充许对查找到用户进行更新或删除操作.如果查找不成功,则给出相应的提示信息 ...

  6. 常用的Atom插件

    1.simplified-chinese-menu 2.tree-view-finder 3.minimap 4.linter和linter-jshint 5.linter-js-standard 6 ...

  7. XMU 1050 Diffuse Secret 【最短路】

    1050: Diffuse Secret Time Limit: 500 MS  Memory Limit: 64 MBSubmit: 10  Solved: 8[Submit][Status][We ...

  8. beego07----web博客

    conf/app.conf appname = blog1 httpport = 8080 runmode = dev name=admin pwd=admin controllersmy/attac ...

  9. O(n²)、O(n)、O(1)、O(nlogn)

    大体上和 @丁戍 说的差不多. 简单说O(n²)表示当n很大的时候,复杂度约等于Cn²,C是某个常数,简单说就是当n足够大的时候,n的线性增长,复杂度将沿平方增长. O(n)也是差不多的意思,也就是说 ...

  10. 《Visual C++ 2010入门教程》系列四:VC2010中初学者常见错误、警告和问题

    <Visual C++ 2010入门教程>系列四:VC2010中初学者常见错误.警告和问题   这一章将帮助大家解释一些常见的错误.警告和问题,帮助大家去理解和解决一些常见问题,并了解它的 ...