Changing Navigation drawer icon on action bar andr

2019-01-18 09:05发布

I have created a NavigationDrawer in my app using the ActionBar. enter image description here

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

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

8条回答
Summer. ? 凉城
2楼-- · 2019-01-18 09:29

call super class methods of ActionBarDrawerToggle super.onDrawerClosed(view) and 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
         }
 }
查看更多
▲ chillily
3楼-- · 2019-01-18 09:33

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);
}
查看更多
登录 后发表回答