说明

  Fragment是在Android3.0(即API 11)才出现的,如果在之前的版本要使用,需要添加support库。

  Fragment可以认为是Actvity模块化的组件,可以很方便地被添加,删除,替换到Activity的视图结构中。通过FragmentTransaction来执行这些操作。

  Fragment还有个重要的特点是:拥有自己的生命周期,受Activity直接影响。

例子

  MainActivity直接继承了FragmentActivity(support库中的)。

  因为API 11之前的Activity无法获取到FragmentManager。在FragmentActivity中可以使用getSupportFragmentManager()。

  如果使用的是API 11以上的,可直接从Activity.getFragmentManager()获得,即无需继承FragmentActivity了。

向Activity添加Fragment的方式:

  1.Activity布局文件中使用<Fragment>属性;

  2.代码中,通过FragmentTransaction可以添加、删除、替换Fragment。

下面的例子,是在代码中添加Fragment。

 package com.dann.fragmentdemo;

 import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MainActivity extends FragmentActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);// Activity的布局 Fragment fragment = new MyFragement();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();// 打开事务
transaction.add(R.id.fragment_container, fragment);// 向View容器添加Fragment。两个参数:容器(ViewGroup)的标识,添加的fragment
transaction.commit();// 提交事务 } /**
* 如果Fragment是有界面的(可以没),就需要继承onCreateView()方法,返回表示Fragment界面的View
*
*/
public static class MyFragement extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.fragment_layout, null); return root;
} }
}

layout_main.xml

  根View是LinearLayout,背景色为红色;两个子View,分别是LinearLayout和FrameLayout,大小相同,其中的FrameLayout是提供Fragment存放的容器。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:orientation="vertical" > <LinearLayout
android:id="@+id/lin_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" /> <FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</FrameLayout> </LinearLayout>

fragment_layout.xml

  根View是LinearLayout,背景色为蓝色。有一个TextView,一个Button。

 <?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="#0000FF"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_fragment" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_fragment" /> </LinearLayout>

执行结果:

  可以看到,我自定义的MyFragment添加到了Activity布局中的FrameLayout里(蓝色部分)。因为FrameLayout本来应该是红色的,但Fragment添加进去后,背景色与Fragment一致(即蓝色)。

代码:http://yunpan.cn/QnLn2LzKA8aEd (访问密码:bd8c)

Android Fragment学习(一)的更多相关文章

  1. Android Fragment学习笔记(二)----Fragment界面添加和管理

    Fragment界面添加 了解过fragment的生命周期等简单知识,于是去看官方文档来了解更多相关内容,要添加fragment到我们的UI界面中,给出了两种常用的方法,第一个是在activity的布 ...

  2. 33.Android之Fragment学习

    Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...

  3. Android之Fragment学习笔记①

    Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...

  4. android fragment 博客 学习记录

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和 ...

  5. Android Fragment应用实战

    现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...

  6. Android Fragment应用实战,使用碎片向ActivityGroup说再见

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...

  7. 【转】基于Android Fragment功能的例子

    原文网址:http://blog.csdn.net/eyu8874521/article/details/8252216 通过最近空闲时候对Fragment的学习,尝试着写了一个小Demo,将在开发的 ...

  8. Android WiFiDirect 学习(二)——Service Discovery

    Service Discovery 简介 在Android WifiDirect学习(一 )中,简单介绍了如何使用WifiDirect进行搜索——连接——传输. 这样会有一个问题,那就是你会搜索到到附 ...

  9. 我的Android 4 学习系列之创建用户基本界面

    目录 使用视图和布局 理解Fragment 优化布局 创建分辨率无关的用户界面 扩展.分组.创建和使用视图 使用适配器将数据绑定到视图 使用视图和布局 1. Android UI 几个基本概念 视图: ...

随机推荐

  1. Maven使用本地jar包(小私服?支持自动打入war包)

    1.库目录结构 D:\maven-local-repo\cn\xcf007\MD5\1.0\MD5-1.0.jar 2.安装到该本地库 mvn install:install-file -Dfile= ...

  2. centos7初步配置

    centos7初步配置 首先安装lrzsz zip/unzip yum -y install lrzsz yum -y install zip unzip 安装vim yum install vim* ...

  3. Sql之表的连接总结

    1.交叉连接(就是将两张表的数据 交叉组合在一起) 有两张表 客户表:[Sales.Customers] 和订单表:[Sales.Orders]. 业务需求:实现 Customer中custid(客户 ...

  4. python学习笔记3(字符串)

    Python字符串: 在Python中的字符串被确定为一组连续的字符在引号之间, Python允许在任何对单引号或双引号. 串的子集,可以使用切片操作符可采用([]和[:]),索引从0开始的字符串的开 ...

  5. OnlineJudge大集合

    什么是OJ Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交程序源代码,系统对源代码进行编译和执行,并通过预先设计的测试数据来检验程序源代码的正确性. 一个用户提交的程序 ...

  6. linux 添加用户

    1.下面就开始来说怎么添加新用户 我们以添加一个用户名为jorcen的新用户为例来说明,执行下面的命令: # useradd jorcen 2.那么就新用户就添加完成,但是没有任何信息,新用户添加完成 ...

  7. 免费素材:25套免费的 Web UI 设计的界面元素(转)

    Web 元素是任何网站相关项目都需要的,质量和良好设计的元素对于设计师来说就像宝贝一样.如果您正在为您的网站,博客,Web 应用程序或移动应用程序寻找完美设计的网页元素,那么下面这个列表会是你需要的. ...

  8. hdu 1233

    最小生成树 本来挺简单  一个小错wa了好几遍 /************************************************************************* & ...

  9. 如何助力企业 APP 在竞争中占据先机?

    做好产品的六字真言:刚需.痛点.高频 --周鸿祎 好的产品是需要不断打磨的.在开发任何产品之前,都需要进行严格的假设和调研,找到刚需,找到痛点.然后就是不断的验证自己的假设,不断地在适当的试错过程中成 ...

  10. Maven的Dependency怎么找?

    用了Maven,所需的JAR包就不能再像往常一样,自己找到并下载下来,用IDE导进去就完事了,Maven用了一个项目依赖(Dependency)的概念,用俗话说,就是我的项目需要用你这个jar包,就称 ...