我有這種形式爲什麼我的碎片出現在彼此之上?
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
的主要佈局和我做FragmentTransaction呼籲他們兩人.replace()
調用使用以下XML兩個片段:
Fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true">
...
</LinearLayout>
Fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
爲什麼這兩個片段相互重疊?我希望Fragment1直接出現在具有RecyclerView的Fragment2上方。
編輯:替換代碼:
fragment1 = Fragment1.newInstance();
fragment2 = Fragment2.newInstance();
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment1, "fragment1");
fragmentTransaction.replace(R.id.fragment2, fragment2, "fragment2");
fragmentTransaction.commit();
或者我這樣做是錯誤的,我應該使用的東西以外的替換? – KaliMa
顯示你的代碼,如何添加片段,幷包括一個截圖,使其更清晰。 – Enzokie
@Enzokie添加了一些代碼 – KaliMa