更改操作栏android上的导航抽屉图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24482825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing Navigation drawer icon on action bar android
提问by user3713706
I have created a NavigationDrawer in my app using the ActionBar.
我使用 ActionBar 在我的应用程序中创建了一个 NavigationDrawer。
As showed in the picture above I want to change the NavigationDrawer toggle button icon to something I want. How can I change it?
如上图所示,我想将 NavigationDrawer 切换按钮图标更改为我想要的东西。我怎样才能改变它?
Here is my code:-
这是我的代码:-
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);
}
If I try changing it to R.drawable.hamburger_button
It still shows the default icon
如果我尝试将其更改为R.drawable.hamburger_button
它仍然显示默认图标
回答by Mateus Gondim
To replace the drawer indicator icon with your own drawable(non animated) using the v7 ActionBarDrawerToggle, you can do the following:
要使用 v7 ActionBarDrawerToggle 用您自己的可绘制(非动画)替换抽屉指示器图标,您可以执行以下操作:
//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);
}
}
});
回答by Msp
Try changing the icon manually by using setHomeAsUpIndicator().
尝试使用setHomeAsUpIndicator()手动更改图标。
Like,
喜欢,
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);
and
和
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(...){};
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer_toggle);
回答by Shivam Verma
Please make sure you include these to sync the states of the icon properly.
请确保包含这些以正确同步图标的状态。
@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);
}
回答by shobhan
call super class methods of ActionBarDrawerToggle
super.onDrawerClosed(view)
and super.onDrawerOpened(drawerView)
like
调用父类的方法ActionBarDrawerToggle
super.onDrawerClosed(view)
和super.onDrawerOpened(drawerView)
像
mDrawerToggle = new ActionBarDrawerToggle(...){
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
//---your code
}
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
//---your code
}
}
回答by Ahmed Raafat
Under the initialization of ActionBarDrawerToggle
write the following code:
在初始化下ActionBarDrawerToggle
写如下代码:
toolbar.setNavigationIcon(R.drawable.ic_menu_camera);
回答by Kaio Cezar Pereira de Oliveira
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
)
回答by shiva yekta
First of all you should in manifest try this code :
首先,您应该在清单中尝试以下代码:
android:icon="@drawable/ic_icon1"
This is image for total logo of your app
这是您的应用程序总徽标的图像
android:logo="@drawable/ic_drower"
This is image for action bar
这是操作栏的图像
After that in main-activity try this code :
之后在主要活动中尝试以下代码:
actionBar.setDisplayUseLogoEnabled(true);
回答by saunlogan
For me adding the setHomeAsUpIndicator worked.
对我来说,添加 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);