レイアウト mylayout.xml を宣言します。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainBack"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/transparent" >
<FrameLayout
android:id="@+id/gridViewFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
FrameLayout
上記のレイアウトを myfragment.xmlに置き換えたいとします (例):
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
次に、コード内の適切な場所でメソッドを呼び出して FrameLayout を交換し、それを GridView に置き換えます (他の場所で実行できるかどうかはわかりません)。
private void attachGridViewFragment(int headerFrag) {
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xact = fragMgr.beginTransaction();
try {
if (findViewById(headerFrag) != null) {
Resources res = getResources();
String title = res.getString(R.string.app_icon_name);
xact.replace(headerFrag, new GridViewFragment(title, true, true), HEADER_FRAGMENT_TAG).commitAllowingStateLoss();
}
} catch (NotFoundException e) {
// TODO Auto-generated catch block
Log.e("MyActivityName", e.getMessage());
}
}
もちろん、GridViewFragment
クラスを作成する必要があります...フラグメントの使用方法については、 Googleだけでできます。
しかし、事実上、GridViewFragment
(たとえば) を拡張するクラスを作成する必要がありますFragment
。