安卓碎片替换

it2023-12-20  61

// 碎片替换 private void replaceFragment(Fragment fragment) { // 碎片管理器 FragmentManager fm=getSupportFragmentManager(); // 碎片事务 FragmentTransaction ft=fm.beginTransaction(); ft.replace(R.id.right_layout,fragment); // 返回 ft.addToBackStack(null); ft.commit(); } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/left_fragment" android:name="com.wm.franment.LeftFragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> //需要替换碎片的位置 <FrameLayout android:id="@+id/right_layout" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"> </FrameLayout> </LinearLayout>

 

最新回复(0)