Programmatically change menu icon color in Toolbar

2019-05-23 18:05发布

问题:

I've developed an App, where the user can change the theme. I have a navigation view, with a menu icon in Toolbar that is black.

I would like to change that icon, to have it white (on a black theme). I tried this code but it remained black:

myToolbar.setTitleTextColor(Color.WHITE);
ab.setHomeAsUpIndicator(R.mipmap.ic_menu_white_24dp); //ab=ActionBar
ab.setDisplayHomeAsUpEnabled(true);

The title becomes White, but the icon doesn't change.

回答1:

1.Add a completely new icon with your favorite color ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_new);

2.Use this to tint

    Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_menu, null);
    drawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, Color.WHITE);
    ab.setHomeAsUpIndicator(drawable);