2017-06-12 55 views
0

我正在創建導航抽屜。片段Android

當我更換它的任何片段,它內容替換,但框架佈局在後面跑..

所以,當我按在該片段空白屏幕,主要佈局的內容顯示。那麼如何在一些片段替換它時完全移除主佈局?

android.support.v4.app.FragmentManager fm=getSupportFragmentManager(); 
if (id == R.id.nav_categories) 
{ 
    Categories cat=new Categories(); 
    FragmentTransaction transaction=fm.beginTransaction().replace(R.id.relative_layout,cat); 
    transaction.addToBackStack(null); 
    transaction.commit(); 
    /* fm.beginTransaction().replace(R.id.relative_layout,cat).commit(); 
      Toast.makeText(this,"Categories",Toast.LENGTH_LONG).show();*/ 
} 
else if (id == R.id.nav_rating) 
    { 
     Fragment_Rating rat=new Fragment_Rating(); 

     fm.beginTransaction().replace(R.id.relative_layout,rat).commit(); 
     Toast.makeText(this,"Rating",Toast.LENGTH_LONG).show(); 
     FragmentTransaction transaction=fm.beginTransaction().replace(R.id.relative_layout,rat); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 

我試圖消除addtobackstack ...

+3

可能重複[Android的 - 如何更改導航抽屜中的碎片](https://stackoverflow.com/questions/24006181/android-how-to-change-fragments-in-the-navigation-drawer) – Thecave3

+0

爲父佈局設置背景 – MathaN

回答

0

在片段xml文件

只需添加到父視圖

android:background="#ffffff" 
android:clickable="true" 
0

添加背景到您的FrameLayout

<FrameLayout 
     android:id="@id/fragment_B" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#B4B4B4" 
     > 
0
FrameLayout layout = (FrameLayout) findViewById(R.id.frame_layout); 
android.support.v4.app.FragmentManager fm=getSupportFragmentManager(); 
if (id == R.id.nav_categories) 
{ 
    Categories cat=new Categories(); 
    FragmentTransaction 
    transaction=fm.beginTransaction().replace(R.id.relative_layout,cat); 
    transaction.addToBackStack(null); 
    transaction.commit(); 
    layout.serVisibility(View.GONE) // add this line 
} 
else if (id == R.id.nav_rating) 
    { 
    Fragment_Rating rat=new Fragment_Rating(); 

    fm.beginTransaction().replace(R.id.relative_layout,rat).commit(); 
    Toast.makeText(this,"Rating",Toast.LENGTH_LONG).show(); 
    FragmentTransaction 
    transaction=fm.beginTransaction().replace(R.id.relative_layout,rat); 
    transaction.addToBackStack(null); 
    transaction.commit(); 
    layout.serVisibility(View.GONE); // add this line 
}