參考自張澤華視頻html
Fragment是自Android3.0後引入的特性,主要用於在不一樣的屏幕尺寸中展示不一樣的內容。java
Fragment必須被嵌入Activity中使用,老是做爲Activity的組成部分。android
簡單示例:app
一個Activity的界面由2個部分組成,每一個部分分別是一個Fragment。ide
效果圖以下:佈局
一、建立第一個Fragment的界面文件。code
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>
二、建立第一個Fragment的類文件。xml
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); } }
三、建立第二個Fragment的界面文件。htm
<?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>
四、建立第二個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); } }
<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>
六、建立主類。
本步驟的關鍵是用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(); } }
注:
一、對兩個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; }
(1)Android2.3及之前對Fragment的支持。
(2)使用Fragment使不一樣的尺寸的屏幕展示不一樣內容。
(3)Activity與Fragment之間的通訊。