之前学习了什么是activity和如何使用activity。在小屏设备(例如智能手机)上,activity通常会填满整个屏幕,显示构成应用程序用户界面的各个视图。activity本质上是视图的的一个容器。但是,在大屏幕设备(例如平板电脑)上显示activity是,就有些不太合适了。因为屏幕增大了,所以必须排列activity中的所有视图,以便成分利用增大的空间,这导致需要对视图层次做复杂的变动。更好的方法是使用“微活动”,让每个微活动包含自己的一组视图。在运行时,根据持有设备的屏幕方向,一个activity可以包含一个或多个这样的微活动。在Android3.0及更高版本,这种微活动被称为“fragment”。
可以把fragment看做另外一种形式的activity。就像activity一样,创建fragment来包含视图。fragment总是嵌入在activity中的。
碎片为android应用程序用户界面的创建提供了一种灵活的方式。他们可以作为用户界面的基本单元,在activity中动态的添加或移除。从而为目标设备创建最佳的用户体验。
创建新的android项目:命名为Fragment:
在res/layout文件夹中,创建一个xml文件,命名为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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment 1"
android:textSize="25sp"
android:textColor="#000000"
/>
</LinearLayout>
再创建一个xml文件: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:orientation="vertical"
android:background="#FFFE00" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #2"
android:textSize="25sp"
android:textColor="#000000"/>
</LinearLayout>
在main.xml 中添加
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- 为了向activity添加一个fragment,使用了fragment元素 。
每个碎片都有一个唯一的表示符,这可以通过android:id或android:tag属性进行设置-->
<fragment
android:name="net.learn2develop.fragment.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"/>
<fragment
android:name="net.learn2develop.fragment.Fragment2"
android:id="@+id/fragment2"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"/>
</LinearLayout>
在net.learn2developer.fragment包名下添加两个Java文件分别命名为:
Fragment1.java和Fragment2.java :
在Fragment1.Java中:
package net.learn2develop.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**fragment的行为与activity十分相似:他有一个Java类,并从一个Xml文件加载UI。
* 这个Xml文件中包含了在activity中常见的UI元素:Textview、edittext、Button等。fragment的Java类继承Fragment类。
* 除了Fragment基类,fragment还可以继承Fragment的几个子类,例如:DialogFragment、ListFragment、和PreferenceFragmet
* */
public class Fragment1 extends Fragment{
//为了绘制fragment的UI,重写了onCreateView()方法。该方法需要返回一个View对象。
/**
* 使用LayoutInflater对象来增大指定xml文件中的UI
* container参数引用父ViewGroup,即准备用于嵌入碎片的activity
* saveInstanceState参数允许将fragment还原到钱一次保存的状态
* */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container,false);
}
}
在Fragment2.Java中:
package net.learn2develop.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}