以编程方式更改工具栏中的菜单图标颜色 [英] Programmatically change menu icon color in Toolbar

查看:56
本文介绍了以编程方式更改工具栏中的菜单图标颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个应用程序,用户可以在其中更改主题.我有一个导航视图,工具栏中的菜单图标是黑色的.

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.添加带有您喜欢的颜色ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_new);

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

2.使用它着色

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

这篇关于以编程方式更改工具栏中的菜单图标颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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