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

时间:2014-07-22 07:13:32

标签: 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="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>

用另一帧ContactFragment替换上述帧的代码。

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

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

1 个答案:

答案 0 :(得分:0)

我不确定这是否是正确的做法..但它确实有效。 你必须做以下 在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组件