Android app开发中的Fragment入门学习教程_Android

在Android3.0上开始引入了一个新概念叫Fragment。它有自己的布局文件,可以作为组件排布,也可以相互组合去实现不同的布局显示。使用Fragment可以重复利用代码,并且可以满足不同设备尺寸的需求。Fragment不能单独存在,只能存在于Activity中,而一个Activity可以拥有多个Fragment。很重要的一点是,Fragment可以和Activity中的其它组件一起使用,无需重写所有Activity的接口。所以使用Fragment就可以这样来完成上例中“主界面—详细界面”的APP需求。

在手机上是这样显示的:

而在平板上是这样的:

在一个小屏幕的设备上,一个activity通常占据了整个屏幕,同时显示各种UI视图组件。Activity实际上就是视图的容器。然后,当一个activity被显示在一个大屏幕的设备上,例如平板电脑,总会显得有些不适应。因为屏幕太大了,activity中的所有UI组件要充满整个屏幕,这样一来,视图的层次结构就很复杂了。一个更好的办法是使用一种“轻量级”的activity,每个“轻量级”activity包含自己的视图,互不干扰。在运行期间,根据屏幕的方向和尺寸,一个activity可以包含一个或多个“轻量级”activity。在Android3.0以上的版本,这种“轻量级”的activity叫做Fragment.

怎么创建一个Fragment

现在我们了解了Fragment的生命周期了,接着我们就需要知道怎么创建一个Fragment并绑定到Activity中,第一件要做的事就是继承android.app.Fragment来写一个Fragment,假设我们的Fragment叫做Fragment1,创建和定义如下:

public class Fragment1 extends Fragment {
...
}

就像我们上面说的,Fragment只能存在于Activity中,所以我们必须要在某处定义它,有两种方式:
- 直接在xml布局文件中定义;
- 在xml布局文件中定义一个占位符,然后动态地在Activity中操作Fragment;

我们定义Fragment的方式会影响它的生命周期,因为在上述第一种情况下onInflate方法会被调用,而第二种情况下它的生命周期是从onAttach方法开始的。

如果我们在XML文件中定义Fragment的话,我们需要:

<fragment android:id="@+id/f1"
            class="com.survivingwithandroid.fragment.Fragment1"
       android:layout_width="match_parent"
       android:layout_height="20dp"/>

然而如果我们在XML中用占位符的话,需要再做一些工作。

布局框架和Fragment

如果我们在XML布局文件中定义Fragment的话,就不能自由、动态修改Fragment了,还有别的方法可以让我们可以更灵活地操作:使用时需要在XML文件中定义:

<FrameLayout android:id="@+id/fl1"
       android:layout_width="match_parent"
       android:layout_height="200dp"/>

在Activity里面还需要做一点工作,因为我们必须手动初始化Fragment,然后把它“插入”到FrameLayout中。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Fragment2 f2 = new Fragment2();
  FragmentTransaction ft = getFragmentManager().beginTransaction();
  ft.replace(R.id.fl1, f2);
  ft.commit();
}

例子

可以把Fragment想象成Activity的另外一种形式。你创建fragments去包含UI组件,就像创建activities那样。但是,Fragment总是被嵌在Activity中。

下面来通过一个例子看一下流程:

1.创建一个名为Fragments的工程。
2.在res/layout文件夹下,新建一个叫fragment1.xml的文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#00FF00"
  android:orientation="vertical" > 

  <TextView
    android:id="@+id/lblFragment1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="This is fragment #1"
    android:textColor="#000000"
    android:textSize="25sp" /> 

</LinearLayout>

3.在res/layout文件夹下,新建一个叫fragment2.xml的文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FFFE00"
  android:orientation="vertical" > 

  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="This is fragment #2"
    android:textColor="#000000"
    android:textSize="25sp" /> 

  <Button
    android:id="@+id/btnGetText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onClick"
    android:text="Get text in Fragment #1"
    android:textColor="#000000" /> 

</LinearLayout>

4.main.xml中的代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" > 

  <fragment
    android:id="@+id/fragment1"
    android:name="net.learn2develop.Fragments.Fragment1"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1" /> 

  <fragment
    android:id="@+id/fragment2"
    android:name="net.learn2develop.Fragments.Fragment2"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1" /> 

</LinearLayout>

5.新建两个类:Fragment1.java和Fragment2.java。
6.Fragment1.java中的代码。

package net.learn2develop.Fragments; 

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; 

public class Fragment1 extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) { 

    Log.d("Fragment 1", "onCreateView"); 

    // ---Inflate the layout for this fragment---
    return inflater.inflate(R.layout.fragment1, container, false);
  }
}

7.Fragment2.java中的代码。

package net.learn2develop.Fragments; 

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; 

public class Fragment2 extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // ---Inflate the layout for this fragment---
    return inflater.inflate(R.layout.fragment2, container, false);
  }
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
, 安卓
fragment
,以便于您获取更多的相关知识。

时间: 2024-08-30 02:26:30

Android app开发中的Fragment入门学习教程_Android的相关文章

Android app开发中的Fragment入门学习教程

在Android3.0上开始引入了一个新概念叫Fragment.它有自己的布局文件,可以作为组件排布,也可以相互组合去实现不同的布局显示.使用Fragment可以重复利用代码,并且可以满足不同设备尺寸的需求.Fragment不能单独存在,只能存在于Activity中,而一个Activity可以拥有多个Fragment.很重要的一点是,Fragment可以和Activity中的其它组件一起使用,无需重写所有Activity的接口.所以使用Fragment就可以这样来完成上例中"主界面-详细界面&q

Android App开发中创建Fragment组件的教程_Android

你可以认为Fragment作为Activity的一个模块部分,有它自己的生命周期,获取它自己的事件,并且你可以在Activity运行的时候添加或者移除它(有点像你可以在不同的Activity中重用的一个"子Activity").这节课程讲述如何使用Support Library继承Fragment类,所以你的应用程序仍然是兼容运行的系统版本低于Android1.6的设备. 注意:如果你决定你的应用要求的最低的API级别是11或者更高,你不需要使用Support Library,反而能使

Android App开发中创建Fragment组件的教程

你可以认为Fragment作为Activity的一个模块部分,有它自己的生命周期,获取它自己的事件,并且你可以在Activity运行的时候添加或者移除它(有点像你可以在不同的Activity中重用的一个"子Activity").这节课程讲述如何使用Support Library继承Fragment类,所以你的应用程序仍然是兼容运行的系统版本低于Android1.6的设备. 注意:如果你决定你的应用要求的最低的API级别是11或者更高,你不需要使用Support Library,反而能使

app-关于android APP开发中SQLITE数据库的问题

问题描述 关于android APP开发中SQLITE数据库的问题 代码如下,首先请问我写的这段创建数据库的代码有没有错误?然后把这个类的文件放在哪里才对啊,目前程序一打开就是程序已经停止运行,APP的第一个ACTIVITY我设置的就是判断这个库中的某个表中有没有数据.我现在感觉就是程序打开,这个库还没有创建,是不存在的,所以运行不了.请大神指教下我.或者说怎么让APP一开始运行的第一步就是运行以下的代码来创建这个数据库.求指教. //数据库创建类 package com.captain.dao

Android开发笔记之Intent初级学习教程_Android

本文讲述了Android开发笔记之Intent初级学习教程.分享给大家供大家参考,具体如下: 项目创建步骤: New Android Project-> Project name:Intent Build Target:Android 2.2 Application name:IntentDemo Package name:com.b510.intent.activity Create Activity:MainActivity Min SDK Version:8 Finish 1.拨打电话 按

Android编程开发中ListView的常见用法分析_Android

本文实例讲述了Android编程开发中ListView的常见用法.分享给大家供大家参考,具体如下: 一.ListView的使用步骤 ListView的使用通常有以下三个要素: (1)ListView中每个条目的布局; (2)填充进入ListView中的内容; (3)将内容与页面进行整合的Adapter. 因此,使用ListView也通常有以下三个步骤: (1)创建ListView条目的布局文件(或使用Android SDK提供的布局); (2)创建填充进入ListView中的内容,如字符串.图片

Android应用开发中使用Fragment的入门学习教程_Android

  Fragment是Android honeycomb 3.0开始新增的概念,Fragment名为碎片不过却和Activity十分相似,下面介绍下Android Fragment的作用和用法.Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多个fragment在一个单独的activity中建立多个UI面板,同时重用fragment在多个activity中.你可以认为fragment作为一个activity中的一节模块 ,fragment有自己的生命周期,接收

Android应用开发中使用Fragment的入门学习教程

Fragment是Android honeycomb 3.0开始新增的概念,Fragment名为碎片不过却和Activity十分相似,下面介绍下Android Fragment的作用和用法.Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多个fragment在一个单独的activity中建立多个UI面板,同时重用fragment在多个activity中.你可以认为fragment作为一个activity中的一节模块 ,fragment有自己的生命周期,接收自己

浅谈Android app开发中Fragment的Transaction操作_Android

在Android中,对Fragment的操作都是通过FragmentTransaction来执行.而从Fragment的结果来看,FragmentTransaction中对Fragment的操作大致可以分为两类: 显示:add() replace() show() attach() 隐藏:remove() hide() detach() 对于每一组方法,虽然最后产生的效果类似,但方法背后带来的副作用以及对Fragment的生命周期的影响都不尽相同. add() vs. replace()只有在F