2016-09-30 35 views
0

我有這種形式爲什麼我的碎片出現在彼此之上?

<?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(); 
+0

或者我這樣做是錯誤的,我應該使用的東西以外的替換? – KaliMa

+0

顯示你的代碼,如何添加片段,幷包括一個截圖,使其更清晰。 – Enzokie

+0

@Enzokie添加了一些代碼 – KaliMa

回答

1

這是因爲你把兩個佈局內CoordinatorLayoutCoordinatorLayout的行爲如RelativeLayout。試試這個。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <FrameLayout 
     android:id="@+id/fragment1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
     <FrameLayout 
     android:id="@+id/fragment2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 
</android.support.design.widget.CoordinatorLayout> 
+0

如果它像RelativeLayout它似乎並沒有讓我做整個「layout_above」或「下面」的東西,這是正確的嗎? – KaliMa

+0

你的修復工作順便說一句,你能解釋更多如何工作? – KaliMa

+0

我的意思是它的行爲像'RelativeLayout',但不是固有的。它用於不同的目的。 – Joshua

0

可以導入片段直接到活動的佈局:

<fragment 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    class="com.packacge.Fragment1"/> 

<fragment 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    class="com.packacge.Fragment2"/> 
+0

是的,但後來我可能想換掉那些地區的其他碎片;現在我只是試圖讓這種情況下工作 – KaliMa

相關問題