2016-11-24 30 views
-2

我剛開始使用Android Studio,對我而言,一切都相對較新。我爲我的應用程序添加了一個新的導航抽屜活動,但它似乎沒有我的欄上的切換按鈕。我會去啓用它。我可以在導航抽屜的類中看到已經存在切換代碼。我已經在互聯網上搜索了很長時間,我發現的所有內容都是將導航抽屜首先作爲其起始模板的教程,或者是從頭開始主要活動的直接創建。將導航抽屜添加到Android Studio中的我的應用程序

我的主要活動:

public class MainActivity extends AppCompatActivity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


    } 

    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.top_menu, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     if(id == R.id.info) { 
      Toast.makeText(this,"Information", Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(this, Information.class); 
      startActivity(intent); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

的抽屜式導航(還未觸碰):

public class navigation_drawer extends AppCompactActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

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

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     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.addDrawerListener(toggle); 
     toggle.syncState(); 

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

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.navigation_drawer, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

回答

0

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

getSupportActionBar()setDisplayShowHomeEnabled(真);

 setSupportActionBar(toolbar); 
+0

嗨了!感謝您的快速回復。我添加了 'getSupportActionBar()。setDisplayHomeAsUpEnabled(true);' 'setSupportActionBar(toolbar);'但我的酒吧裏依然沒有任何東西。然後,我添加了 getSupportActionBar()。setDisplayHomeAsUpEnabled(true); 在我的主要活動中的onCreate方法下,它出現了,但無法點擊。我是否需要將我的導航器從我的主體中擴展出來? –

+0

你還加了'getSupportActionBar()。setDisplayShowHomeEnabled(true);' –

+0

是的,最後,我開始了一個新項目,選擇抽屜模板,並將所有代碼放在新項目的主要活動中。我認爲我的問題是無法連接原始項目中的兩個java文件。我的所有應用程序代碼都放在我的主文件夾上,而另一個java文件上的所有導航代碼都放在了。 –