Fragment基础----创建
1,Fragment的目的及应用场景
fragment 是3.0后引入的类,其字面翻译为“碎片”。
目的是将activity划分成许多单元再进行组合,可以根据不同分辨率屏幕,在不同状态下,灵活创建优化UI并提高复用性。

2,Fragment的创建
第一种方式:通过xml标签创建
step 1:创建fragment类继承fragment关系类,其中导包的时候app包为3.0以后使用,v4包可以向下兼容
step 2:在activity的xml文件中添加fragment标签并添加name属性为fragment全路径
<fragment
android:id="@+id/fragment_center"
android:name="singleorb.com.fragment.FragmentCenter"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="5" /> <fragment
android:id="@+id/fragment_bottom"
android:name="singleorb.com.fragment.FragmentBottom"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
step 3:创建fragment类的布局文件,
<?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:orientation="vertical"
android:background="#00ff11"> </LinearLayout>
<?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:orientation="vertical"
android:background="#ff0000"> </LinearLayout>
step 4:并在onCreateView中添加
public class FragmentBottom extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_xml_bottom, container, false);
return view;
}
}
public class FragmentCenter extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_xml_center, container, false);
return view;
}
}
step 5:启动即可看到结果,把activity分成两部分

第二种方式:通过Java代码方式创建
1,只需把第一种方法的fragment标签换成FramLayout去掉name属性
<FrameLayout
android:id="@+id/fragment_code_center"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="5" /> <FrameLayout
android:id="@+id/fragment_code_bottom"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
2, 然后再在activity中添加相应引入代码即可
/*
*java 代码动态添加fragment 需要两个操作对象
* FragmentManager
*FragmentTransaction
*/
//获取FragmentManager实例
FragmentManager fragmentManager = getFragmentManager();
//开启事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//添加布局文件
fragmentTransaction.add(R.id.fragment_code_center, new FragmentCenter());
fragmentTransaction.add(R.id.fragment_code_bottom, new FragmentBottom());
//提交
fragmentTransaction.commit();
得到的结果和用xml创建的一样,但是用Java代码来创建可以更灵活的使用所以推荐应用Java代码方式创建
Fragment基础----创建的更多相关文章
- 札记:Fragment基础
Fragment概述 在Fragment出现之前,Activity是app中界面的基本组成单位,值得一提的是,作为四大组件之一,它是需要"注册"的.组件的特性使得一个Activit ...
- android studio 2.2.2下fragment的创建和跳转
一,首先,Fragment是android应用中十分重要的一个功能,十分轻量化,也类似于activity一样,是一个个布局,可以相互跳转和传递参数.但是,它运行起来十分流畅,而且易于管理,下面是在学习 ...
- android之Fragment基础详解(一)
一.Fragment的设计哲学 Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕比手机的大得多,有 ...
- Fragment基础操作
Fragment和Activity类似,同样是具备UI的属性:也就是都能用于规划UI布局... Building a Dynamic UI with Fragments --> Fragment ...
- android开发(2):多页面的实现 | Fragment的创建与使用
APP中出现多个页面再常见不过了.使用activity与fragment都能实现多页面,这里使用fragment来实现.延续“知音”这个APP的开发,之前已经创建了底部导航条与mainactivity ...
- fragment基础 fragment生命周期 兼容低版本
fragment入门 ① 创建一个类继承Fragment 重写oncreateView方法 public class FirstFragment extends Fragment { @Overrid ...
- Fragment的创建与通信
由于这里涉及到接口回调的问题,所以先来看一看什么是接口回调: 这就好比老板和员工的微妙关系,老板需要员工去工作,员工挣钱了以后还要告诉老板自己挣了多少钱,然后由老板来处理这些钱. 首先创建一个接口: ...
- Fragment基础----信息传递
如何通过activity来访问fragment的信息呢,我们可以通过一个实例来了解. 使用两个Fragment和一个activity管理 效果图: 第一个fragment来表示输入框 第二个fragm ...
- Android Studio 单刷《第一行代码》系列 05 —— Fragment 基础
前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...
随机推荐
- 新版 itextsharp pdf code
using System; using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; namespace iTextShar ...
- 实用redis前需了解的5大事项
百万个键,每个值的长度是32-character,那么在使用6-character长度键名时,将会消耗大约96MB的空间,但是如果使用12-character长度的键名时,空间消耗则会提升至111MB ...
- Android课程---远程服务器存储
在使用Volley进行获取数据时,需要事先准备环境:在libs里面导入一个Volley.jar包,在网上都有,可以下载下来导入,或者自己电脑上有DT的,自己合成一个包也行. WebActivity.j ...
- 常用SQL[ORACLE]
1.常用系统函数 2.常用sql语句 3.一些定义和关键字 4.需要注意点 1.常用系统函数 ↑ --decode decode(column,if_value,value,elseif_ ...
- iconfont的蜕化操作
很多国外的网站,访问的时候可以看到,页面先是大面积白一下,然后恢复正常.原因是网页上用到了 webfont,这些页面很多情况都是直接引用 google 的 webfont 地址,中华大局域网下,由于网 ...
- 【Java并发编程实战】-----“J.U.C”:Semaphore
信号量Semaphore是一个控制访问多个共享资源的计数器,它本质上是一个"共享锁". Java并发提供了两种加锁模式:共享锁和独占锁.前面LZ介绍的ReentrantLock就是 ...
- Qt5 Crash When Open File With QFileDialog
问题描述 在使用Qt的QFileDialog这个类,来进行文件的打开和选择的时候, 就在调用的时候, 总是发生崩溃. 而且没有任何的提示性的信息. 而且崩溃的概率很高. 也有不崩溃的情况. 这个问题, ...
- SharedPreferences.Editor 的apply()与commit()方法的区别
commit()的文档 官方文档如下: Commit your preferences changes back from this Editor to the SharedPreferences o ...
- 前端编码规范之JavaScript
上次浅谈了下关于CSS的编码规范,大部分童鞋持赞同意见,仍存在一些童鞋不太理解这些规范的意义. 如果是个人或者小作坊开发,其实这些所谓的编码规范也没啥意思,因为大家写好的代码直接就给扔到网上去了,很少 ...
- Selenium WebDriver 3.0 需要注意的事项
以下所有代码基于Java 首先,要使用WebDriver 3.0 的话 请使用JAVA 8(必要) 其次,由于W3C标准化以及各大浏览器厂商的积极跟进,自WebDriver 3.0 之后,Sele ...