Fragment:
又稱片段,用在Activity的局部,消除局部佈局代碼的重複。
使用步驟:
創建Fragment:1)新建一個XML佈局(在Activity裏要使用的);
2)新建一個class,繼承Fragment(android.support.v4.app.Fragment);
3)重寫onCreateView()方法,關聯1裏的佈局;return inflater.inflate();
使用Fragment:1)新建的Activity繼承FragmentActivity;
2)在佈局中增加FrameLayout(要使用Fragment的地方)並添加id;
3)在Activity中實現:
//得到管理器;
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = new MyFragment();
ft.add(R.id.FrameLayoutid, fragment);
ft.commit();
以上是Fragment的基本用法,希望能幫您更好的學會它;