2016-11-10 156 views
0

我想用一個抽屜本次活動如何添加片段主要活動

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener, ActionBar.TabListener{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     //codi 

    } 
+0

*但我在這個活動中使用抽屜* ...所以?這是如何阻止顯示片段的? –

+0

我不能'在主要活動中使用擴展片段,我知道如何調用片段 – Mouna14

+0

您是否嘗試閱讀一些教程?另外,我認爲Android Studio創建的「NavigationView」模板包含Fragments。 –

回答

0

就像在評論中提到添加一個名爲今天片段在我的主要活動 但是我,你應該閱讀教程關於那個。放進了oncreate()方法

getFragmentManager().beginTransaction() 
       .add(R.id.container, yourFragment()        
       .commit(); 

add()方法的第一個放慢參數是在的ViewGroup活動的XML。應放置片段(R.layout.activity_main在您的案例中)。