色调菜单图标色调、图标、菜单

2023-09-04 11:23:26 作者:幸福便会来临

我要建一个Android应用程序,我用从操作栏图标包的图标操作栏中使用。我通过菜单文件夹中的XML文件中定义它们。 有没有一种方法来色调这些图标,使他们都是一样的颜色? 到目前为止,我必须用图像编辑软件,做手工,但如果我决定改变颜色,我要全部做一遍。

I'm building an Android app and I use the icons from the Action Bar Icon Pack to use in the action bar. I define them through the xml files in the menu folder. Is there a way to "tint" these icons so that they are all the same color? So far, I have to do it manually with an image editing software but if I decide to change the color, I have to do it all over again.

我知道有一个安卓色调属性的ImageView ,但我还没有找到一个方法来使用它的菜单的图标。

I know there is a android:tint attribute for ImageView but I haven't found a way to use it for the menu's icons.

感谢

推荐答案

有可能是一个更好的办法做到这一点,但其中一个方案是重新绘制在code中的图标。

There may be a better way to do this, but one option is to redraw the icon in code.

假设你有一个收藏夹菜单项,并希望其着色灰色的:

Suppose you have a menu item for favorites and want to tint it gray:

MenuItem favoriteItem = menu.findItem(R.id.action_favorite);
Drawable newIcon = (Drawable)favoriteItem.getIcon();
newIcon.mutate().setColorFilter(Color.argb(255, 200, 200, 200), PorterDuff.Mode.SRC_IN);
favoriteItem.setIcon(newIcon);

您也可以使用像

newIcon.mutate().setColorFilter(getResources().getColor(R.color.myCustomTint), PorterDuff.Mode.SRC_IN);