2016-09-30 36 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

相关问题