我有一个 Android 布局文件和一个 Fragment 类。如何将 Fragment 添加到布局文件中?
安卓布局文件
<?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>
片段类
public class PanelDerechoLowerFragment extends Fragment {
...
}
XML 中的 FrameLayout。然后,您必须以编程方式在活动中添加片段。
Activity 将与您的 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();`
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句