Java 如何将片段插入到布局xml文件中?
我有一个Android布局文件和一个片段类。如何将片段添加到布局文件中 Android布局文件Java 如何将片段插入到布局xml文件中?,java,android,android-fragments,Java,Android,Android Fragments,我有一个Android布局文件和一个片段类。如何将片段添加到布局文件中 Android布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="346dp" android:layout_height="match_parent" a
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="346dp"
android:layout_height="match_parent"
android:background="@drawable/botonera_registradora_bg"
android:gravity="center_horizontal">
...
==> here I need to put my fragment
...
</RelativeLayout>
XML中的FrameLayout。然后,您必须以编程方式在活动中添加片段 该活动将与您的RelativeLayout关联,片段将有自己的布局 这样,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="346dp"
android:layout_height="match_parent"
android:background="@drawable/botonera_registradora_bg"
android:gravity="center_horizontal">
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="346dp"
android:layout_height="match_parent"
android:background="@drawable/botonera_registradora_bg"
android:gravity="center_horizontal">
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
ItemDetailFragment itemDetailFragment=new ItemDetailFragment();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
transaction.setCustomAnimations(animSlideIn, animSlideOut, animSlideIn, animSlideOut);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).replace(R.id.frameLayout, itemDetailFragment)
.commitAllowingStateLoss();`