標籤:
一直知道Fragment非常強大。可是一直都沒有去學習,如今有些空暇的時間,所以就去學習了一下Fragment的簡單入門。我也會把自己的學習過程寫下來,假設有什麼不足的地方希望大牛指正,共同進步。
一、Fragment簡單介紹
1.Fragment作為Activity介面的一部分組成出現;
2.能夠在一個Activity中同一時候出現多個Fragment,而且,一個Fragment亦可在多個Activity中使用;
3.在Activity執行過程中,能夠加入、移除或者替換Fragment(add()、remove()、replace());
4.Fragment能夠響應自己的輸入事件,而且有自己的生命週期,當然。它們的生命週期直接被其所屬的activity的生命週期影響。
那我們為什麼要用Fragment呢?主要目的是用在大螢幕裝置上--比如平板電腦上,支援更加動態和靈活的UI設計。平板電腦的螢幕要比手機的大得多,有很多其它的空間來放很多其它的UI組件,而且這些組件之間會產生很多其它的互動。我們能夠把Fragment覺得是“小的Activity”。Fragment更加簡潔。
二、Fragment的簡單使用
那我們就簡單的顯示2個Fragment為例來解說一下。
1.在XML中加入Fragment:
建立Fragment1、Fragment2(注意:這裡可能有2個包能夠選擇匯入android.app.Fragment或android.support.v4.app.Fragment都是能夠的,我這裡選擇使用了前者。可是兩者使用時有差別的,在結尾中我會講到):
Fragment1代碼:
package com.example.fragment;import android.app.Fragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.example.fragmentdemo.R;public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.e("TAG", "in"); return inflater.inflate(R.layout.fragment1, container, false); }} Fragment2代碼:
package com.example.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.example.fragmentdemo.R;public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, container, false); }} Fragment1的xml代碼:
<?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="#FF69B4" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="這是第一個Fragment" /></LinearLayout>
Fragment2的xml代碼:
<?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="#EECBAD" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="這是第二個Fragment" /></LinearLayout>
我們在activity_main.xml中加入兩個Fragment。代碼例如以下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" > <fragment android:id="@+id/fragment1" android:name="com.example.fragment.Fragment1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="com.example.fragment.Fragment2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" /></LinearLayout>
MainActivity代碼例如以下:
package com.example.fragmentdemo;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
然後執行project就能夠顯示Fragment了。以下是。
2.動態加入Fragment:
我們僅僅須要改動MainActivity和activity_main.xml中的代碼就能夠了。
activity_main.xml代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="vertical" > <LinearLayout android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" /></LinearLayout>
MainActivity代碼:
package com.example.fragmentdemo;import android.app.Activity;import android.os.Bundle;import com.example.fragment.Fragment1;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getFragmentManager().beginTransaction() .replace(R.id.main, new Fragment1()).commit(); }}
然後執行project就能夠動態顯示Fragment了,以下是。
三、app包下和V4包下的Fragment的差別
1、盡量不要用app包中的fragment。由於這個是在3.0之後才有的,支援的版本號碼太高,在低版本號碼中是是用不了的;
2、android.support.v4.app.Fragment:能夠相容到1.6的版本號碼。
3、關於這兩個fragment使用<fragment>標籤的問題:
(1)app.fragment和v4.fragment都是能夠使用<fragment>標籤的僅僅是在在使用的時候假設是app.fragment則沒有什麼特殊的地方繼承Activity就可以。
(2)當v4.fragment使用<fragment>標籤的時候就要特別注意了:當這個Activity的布局中有<fragment>標籤的時候,這個Activity必須繼承FragmentActivity,否則就會報錯。
此時假設不蔔繼成FragmentActivity的話 編譯系統會把<fragment>覺得是app包中的Fragment來處理。可是此時我們匯入的是v4包中的FragmentAndroid官方文檔中的Fragment的範例就是以app包中的Fragment來解說的。
(3)app包中關於Fragment的類和方法在V4包中都是有相應的相應的。
轉載自:http://blog.csdn.net/a465456465/article/details/10415211,感謝。
上面就是Fragment的簡單用法,Demo下載,下一節我會講Fragment的具體使用。歡迎關注。我的部落格園地址:http://www.cnblogs.com/getherBlog/p/3943547.html。
【Android發展】它Fragment發展1