標籤:android開發 fragment fragment簡介 fragment的使用
一直知道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); }}
然後運行工程就可以顯示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(); }}
然後運行工程就可以動態顯示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