更改操作栏android上的导航抽屉图标 [英] Changing Navigation drawer icon on action bar android

查看:1018
本文介绍了更改操作栏android上的导航抽屉图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ActionBar在我的应用中创建了一个NavigationDrawer。

I have created a NavigationDrawer in my app using the 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?

这是我的代码: -

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);
}

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

If I try changing it to R.drawable.hamburger_button It still shows the default icon

推荐答案

用您自己的drawable替换抽屉指示器图标(非动画)使用v7 ActionBarDrawerToggle,您可以执行以下操作:

To replace the drawer indicator icon with your own drawable(non animated) using the v7 ActionBarDrawerToggle, you can do the following:

//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);
        }
    }
});

这篇关于更改操作栏android上的导航抽屉图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆