您将声明您的布局 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
课程......您可以在谷歌上搜索如何使用片段。
但实际上你必须创建一个GridViewFragment
扩展的类(例如)Fragment
。