我正在使用android studio 3.1.3,但没有找到错误,我按照教程进行操作,但是在我的情况下,它无效,我搜索了源代码,却没有发现任何错误,并找到了其他链接: https://github.com/CursoAndroidMaterialDesign/Navigation-Drawer
请帮助
主要活动:
package com.example.italo.nvigationdrawer;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout,toolbar,R.string.open_drawer,R.string.close_drawer);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.menu_inbox:{
Toast.makeText(this,"Menu Inbox" , Toast.LENGTH_SHORT).show();
break;
}
case R.id.menu_starred:{
Toast.makeText(this,"Menu Favoritos", Toast.LENGTH_SHORT).show();
break;
}
case R.id.menu_sent_email:{
Toast.makeText(this, "Menu Enviados", Toast.LENGTH_SHORT).show();
break;
}
case R.id.menu_trash:{
Toast.makeText(this, "Menu Lixeira", Toast.LENGTH_SHORT).show();
break;
}
case R.id.menu_spam:{
Toast.makeText(this, "Menu Spam", Toast.LENGTH_SHORT).show();
break;
}
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
主要活动xml:
<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/drawerLayout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_nav"
/>
<include layout="@layout/toolbar"/>
菜单类:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item android:title="@string/inbox"
android:id="@+id/menu_inbox"
android:icon="@drawable/ic_inbox"/>
<item android:title="@string/favoritos"
android:id="@+id/menu_starred"
android:icon="@drawable/ic_starred"/>
<item android:title="@string/emails_enviados"
android:id="@+id/menu_sent_email"
android:icon="@drawable/ic_sent_email"/>
</group>
<item android:title="@string/outros"
android:id="@+id/submenu_outros">
<menu>
<item android:title="@string/lixeira"
android:id="@+id/menu_trash"
android:icon="@drawable/ic_trash"/>
<item android:title="@string/spam"
android:id="@+id/menu_spam"
android:icon="@drawable/ic_spam"/>
</menu>
</item>
</menu >
结果:
答案 0 :(得分:0)
您必须在formatter.currencyGroupingSeparator = ","
formatter.currencyDecimalSeparator = "."
的根级别添加menu
Menu class:
答案 1 :(得分:0)
在您的onNavigationItemSelected
方法中,尝试用break;
语句替换return true;
答案 2 :(得分:0)
use this code in your drawer project.
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (menuItemWaiting != null) {
onNavigationItemSelected(menuItemWaiting);
}
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
and then add define
onNavigationItemSelected( final MenuItem item ){
menuItemWaiting = null;
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
menuItemWaiting = item;
drawerLayout.closeDrawers();
return false;
}
int id = item.getItemId();
switch (id) {
case R.id.nav_home:
fragment = new HomeFragment();
drawerLayout.closeDrawer(Gravity.START);
break;
}
}