1.首先是當android2.3.3之前還是用着android-support-v4.jar來加載Fragment時。
a.在xml布局應該如何定義呢?
答案:用FrameLayout標簽來定義(在android 3.0之后的版本就是好似用fragment標簽)
本人嘗試用其他Layout,但是類似不行(不知道是不是人品問題,不過用FrameLayout的話100%可以)
然后就以下面的代碼來加載Fragment!
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.my_frameLayout,new Fragment());
transaction.commit();
2.本人曾經出現一個問題:就是布局在上面的Fragment(就是FrameLayout)死活都要蓋住他的下面的Fragment的內容。
當時一時半刻不管怎么改。就是弄不好!
這兩天人品爆發。嘗試把布局換一下位置。居然好了!
我貼一下xml的代碼吧!
之前的代碼:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/title_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/bottom_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</FrameLayout>
</FrameLayout>
</LinearLayout>
更改后:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/title_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
android:id="@+id/content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
android:id="@+id/bottom_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
后來才知道這個FrameLayout(假如加載Fragment的話)里面是盡量不要放子控件的,否則加載Fragment就會把里面的子控件內容擋住。
3.這兩天更新了android-SDK-4.4.2,就順便更新了ADT(ADT不知道是什么的話。我勸你還是別看了)
然后今天打包時突然很多類都打了紅叉。打開一看錯誤是:
This fragment should provide a default constructor (a public constructor with no arguments)
大概意思是說。用這個fargment時。一定要留一個什么參數都不給的的構造方法!
然后我又去看看官方寫的demo。基本都是 有
public static Fragment getInstance(boolean isTest) {
TestFragment instance = new TestFragment();
instance.isTest = isTest;
return instance;
}
類似的static 方法。
大概意思就是說:傳參只能等對象實例化后才能賦值!
這個不算錯誤。只是過不了android的打包工具檢查而已!
也算android強制要求代碼要規范吧!