Android 替换片段而不是替换内容?

Android 替换片段而不是替换内容?,android,Android,包含正在替换的framelayout的XML代码 <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="

包含正在替换的framelayout的XML代码

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="110dp"
        android:layout_marginTop="30dp"
        android:src="@drawable/icon_new"
        android:onClick="new_booking" />

     <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="110dp"           
        android:src="@drawable/icon_history"
        android:onClick="click_history" />
     <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
     <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="30dp"
        android:src="@drawable/icon_rates" 
        android:onClick="click_rates"/>

     <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="80dp"           
        android:src="@drawable/icon_customer_care"
        android:onClick="click_customer" />
    </LinearLayout>
</LinearLayout>
</FrameLayout>

内容框架没有被替换,而是被覆盖…即我能够看到包含在内容框架中的图像…即使在调用事务之后。

我不确定这是否是正确的方法。但它起了作用。 你必须做到以下几点 在java文件中创建FrameLayout的实例

FrameLayout frameLayout = (FrameLayout) findViewById(R.id.content_frame);
然后按如下方式使用:

    android.support.v4.app.FragmentManager fragmentManager =       getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction =      fragmentManager.beginTransaction();
    frameLayout.removeAllViews();
    ContactFragment fragment = new ContactFragment();
    fragmentTransaction.replace(R.id.content_frame, fragment);
    fragmentTransaction.commit();
    mDrawerList.setItemChecked(5, true);
    mDrawerLayout.closeDrawer(mDrawerList);  

frameLayout.removeAllViews()此行删除frameLayout中的所有xml组件

不替换任何frameLayout内容,无论您所做的是在其中加载另一个片段,如果您想这样做,有两种方法:1)将默认布局内容设为片段,并在加载另一个片段时,删除第一个。2) 在将片段加载到此framelayout之前删除所有视图。是的,我将用另一个片段(Conatct片段)替换整个片段(内容片段)…但内容片段的内容也可见。根据我的说法,应该是replacedYour没有删除先前加载的片段,这就是为什么显示先前片段的内容,请尝试以下操作:
transaction.remove(removeFragment);事务.add(R.id.content\u frame,newFragment)。让我知道它是否有效。但“替换”的作用与“删除并添加同一行中的片段”的作用相同…并且我正在使用transaction.replace只需使用一次,然后让我知道。
    android.support.v4.app.FragmentManager fragmentManager =       getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction =      fragmentManager.beginTransaction();
    frameLayout.removeAllViews();
    ContactFragment fragment = new ContactFragment();
    fragmentTransaction.replace(R.id.content_frame, fragment);
    fragmentTransaction.commit();
    mDrawerList.setItemChecked(5, true);
    mDrawerLayout.closeDrawer(mDrawerList);