参考自张泽华视频

Fragment是自Android3.0后引入的特性,主要用于在不同的屏幕尺寸中展现不同的内容。

Fragment必须被嵌入Activity中使用,总是作为Activity的组成部分。

简单示例:

一个Activity的界面由2个部分组成,每个部分分别是一个Fragment。

效果图如下:

1、创建第一个Fragment的界面文件。

Fragment的界面文件和一般的Activity布局文件一样,如。

<?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="match_parent"
        android:layout_height="match_parent"
        android:text="@string/fragment1"
        /> </LinearLayout>

2、创建第一个Fragment的类文件。

package com.ljh.fragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; //继续Fragment类。
public class Fragment1 extends Fragment { @Override
//在Fragment被加载时调用
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//加载某个布局文件。
return inflater.inflate(R.layout.fragment1, null);
}
}

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:background="#00ff00"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/fragment2"/> </LinearLayout>

4、创建第二个Fragment的类文件。

package com.ljh.fragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; //继续Fragment类。
public class Fragment2 extends Fragment { @Override
//在Fragment被加载时调用
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//加载某个布局文件。
return inflater.inflate(R.layout.fragment2, null);
} }

5、创建主界面文件。

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >     <fragment
        android:id="@+id/fragment1"
        android:name="com.ljh.fragmentdemo.Fragment1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />     <fragment
        android:id="@+id/fragment2"
        android:name="com.ljh.fragmentdemo.Fragment2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" /> </LinearLayout>

6、创建主类。

本步骤的关键是用Fragment对象取代布局文件中的内容。

package com.ljh.fragmentdemo;

import com.ljh.fragmentdemo.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //获得FragmentManager,然后获取FragmentTransaction。
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
//用Fragment动态代替布局文件中的内容
transaction.replace(R.id.fragment1, new Fragment1());
transaction.replace(R.id.fragment2, new Fragment2());
//提交事务
transaction.commit();
} }

注:

1、对两个Fragment的分别进行编辑,如

	@Override
//在Fragment被加载时调用
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//加载某个布局文件。
View root = inflater.inflate(R.layout.fragment1, null); TextView tvContent = (TextView) root.findViewById(R.id.tv_content);
tvContent.setText("hello"); return root;
}

2、其它内容见后面博客:

(1)Android2.3及以前对Fragment的支持。

(2)使用Fragment使不同的尺寸的屏幕展现不同内容。

(3)Activity与Fragment之间的通信。

Fragment之一:Fragment入门的更多相关文章

  1. fragment基础 fragment生命周期 兼容低版本

    fragment入门 ① 创建一个类继承Fragment 重写oncreateView方法 public class FirstFragment extends Fragment { @Overrid ...

  2. RadioGroup+Fragment 使用Fragment的add()方法,防止使用replace每次都重新加载页面,造成资源浪费

    radiogroup+fragment是很常用的主页导航控件,之前为了代码简便一直使用的replace()替换fragment,代码如下: getSupportFragmentManager().be ...

  3. android fragment嵌套fragment出现的问题:no activity

    package com.example.fragmentNavigation2.fragment; import android.content.Context; import android.os. ...

  4. 关于Fragment与Fragment、Activity通信的四种方式

    一直想总结一下Fragment与Fragment.Activity通信的问题,今天有时间一共总结了三种,权当抛砖引玉,如果大家还有更好的方式来实现Fragment和Fragment.Activity的 ...

  5. activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI

    今天遇到挺纠结的问题,由于产品设计的问题,技术上涉及到activity 嵌套一级fragment,一级fragment嵌套二级fragment,在一级fragment中刷新二级fragment中的UI ...

  6. TabActivity 、fragemnt+fragment 和 Fragment+Viewpager 优缺点

    1 TabActivity : 1 过时了 . 2 activity . 是作为android的四大组件...                   重量级的家伙   ViewGroup   : 特别麻 ...

  7. Fragment(一)--Fragment用法常见问题

    fragment notes fragment相关内容包括 基本定义与使用 回退栈内部实现 fragment通信(与activity 与fragment) DialogFragment VP + Fr ...

  8. Fragment学习(二): 管理Fragment和Fragment通讯

    一. 管理Fragment 首先,如果你想在Android3.0及以下版本使用Fragment,你必须引用android-support-v4.jar这个包 然后你写的activity不能再继承自Ac ...

  9. Fragment 嵌套Fragment注意事项

    最近项目新功能需要在垂直方方向可以循环滚动,并且水平方向也可以水平循环滚动,并且可以定位到指定item上.很自然的想到了ViewPager和 VerticalViewPager来解决项目需求,UI的大 ...

  10. Android 中关于Fragment嵌套Fragment的问题

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5802146.html 问题描述: 在项目中Activity A中嵌套Fragment B,Fragment ...

随机推荐

  1. a^b的前n位数

    假设我们现在需要知道 ab  的后 n 位数或前 n 位数,简单直观的做法就是求出 ab  的值,然后在分别取前 n位或后 n位,不过在 a,b很大的情况下显然是无法存储的.所以,直接求是不可能的了. ...

  2. Scrapinghub执行spider抓取并显示图片

    序 最近在学习Scrapy的时候发现一个很有意思的网站,可以托管Spider,也可以设置定时抓取的任务,相当方便.于是研究了一下,把其中比较有意思的功能分享一下: 抓取图片并显示在item里: 下面来 ...

  3. 秒(s) 毫秒(ms) 微秒(μs) 纳秒(ns) 皮秒(ps)及Java获得 .

    Date date=new Date(); long hm=date.getTime(); //获取毫秒 或者 毫秒级:System.currentTimeMillis() 纳秒级: System.n ...

  4. ububru下 redmine安装教程

    1.安装之前确认你已经安装好了mysql 2.连接mysql创建数据库 mysql -u root –p CREATE DATABASE redmine CHARACTER SET utf8; CRE ...

  5. 学习第一个头文件stdio.h

    使用标准输入输出库函数时要用到 “stdio.h”文件,因此源文件开头应有以下预编译命令: #include<stdio.h> stdio是standard input&outup ...

  6. pm2安装及常用命令

    安装:npm install -g pm2 启动程序:pm2 start <app_name|id|all> 列举进程:pm2 list 退出程序:pm2 stop <app_nam ...

  7. Hibernate中3种结果转换的详细说明(转)

    Hibernate中3种结果转换的详细说明 在hibernate使用的过程中.我们通常需要对结果进行解释. Hibernate为我们提供了以下3种解释方法: Transformers.ALIAS_TO ...

  8. Java开发者工具

    From:http://www.csdn.net/article/2015-03-26/2824317 1. Notepad++ Notepad++是用于编辑xml.脚本以及记笔记的最佳工具.这个工具 ...

  9. c++容器使用总结(转载)

    目录 ==================================================== 第一章 容器 第二章 Vector和string 第三章 关联容器 第四章 迭代器 第五 ...

  10. 图的深度优先遍历DFS

    图的深度优先遍历是树的前序遍历的应用,其实就是一个递归的过程,我们人为的规定一种条件,或者说一种继续遍历下去的判断条件,只要满足我们定义的这种条件,我们就遍历下去,当然,走过的节点必须记录下来,当条件 ...