之前學習了什麼是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);
}
}