更改操作栏上的导航抽屉图标android

我使用ActionBar在我的应用程序中创build了一个NavigationDrawer。 在这里输入图像说明

如上图所示,我想将NavigationDrawer切换button图标更改为我想要的内容。 我怎样才能改变它?

这是我的代码:

mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); // enabling action bar app icon and behaving it as toggle button getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.hamburger_button, //nav menu toggle icon R.string.app_name, // nav drawer open - description for accessibility R.string.app_name // nav drawer close - description for accessibility ) { public void onDrawerClosed(View view) { getActionBar().setTitle(mTitle); // calling onPrepareOptionsMenu() to show action bar icons invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { getActionBar().setTitle("Settings"); // calling onPrepareOptionsMenu() to hide action bar icons invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggls mDrawerToggle.onConfigurationChanged(newConfig); } 

如果我尝试将其更改为R.drawable.hamburger_button它仍然显示默认图标

Solutions Collecting From Web of "更改操作栏上的导航抽屉图标android"

要使用v7 ActionBarDrawerTogglereplace抽屉指示器图标和您自己的drawable(非animation),您可以执行以下操作:

 //After instantiating your ActionBarDrawerToggle mDrawerToggle.setDrawerIndicatorEnabled(false); Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.your_custom_icon, getActivity().getTheme()); mDrawerToggle.setHomeAsUpIndicator(drawable); mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) { mDrawerLayout.closeDrawer(GravityCompat.START); } else { mDrawerLayout.openDrawer(GravityCompat.START); } } }); 

尝试使用setHomeAsUpIndicator()手动更改图标。

喜欢,

 ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer); 

 ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(...){}; mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer_toggle); 

请确保包括这些以正确同步图标的状态。

 @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } 

调用超类方法的ActionBarDrawerToggle super.onDrawerClosed(view)super.onDrawerOpened(drawerView) like

 mDrawerToggle = new ActionBarDrawerToggle(...){ public void onDrawerClosed(View view) { super.onDrawerClosed(view); //---your code } public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); //---your code } } 
  mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.hamburger_button(This you the icon), //nav menu toggle icon R.string.app_name, // nav drawer open - description for accessibility R.string.app_name // nav drawer close - description for accessibility ) 

首先,你应该在体验这个代码:

 android:icon="@drawable/ic_icon1" 

这是您的应用程序的总标识图像

 android:logo="@drawable/ic_drower" 

这是操作栏的图像

之后,在主要活动尝试下面的代码:

 actionBar.setDisplayUseLogoEnabled(true); 

对于我添加setHomeAsUpIndicator工作。

 mDrawerToggle = new ActionBarDrawerToggle( this, /* host Activity */ mDrawerLayout, /* DrawerLayout object */ R.drawable.custom_icon, /* nav drawer image to replace 'Up' caret */ R.string.drawer_open, /* "open drawer" description for accessibility */ R.string.drawer_close /* "close drawer" description for accessibility */ ) { public void onDrawerClosed(View view) { getSupportActionBar().setTitle(mTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { getSupportActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.custom_icon); 

ActionBarDrawerToggle的初始化下写下面的代码:

 toolbar.setNavigationIcon(R.drawable.ic_menu_camera);