Android汉堡/箭头图标动态改变颜色
我想改变导航抽屉的汉堡/箭头图标的颜色。 我知道我可以在样式中更改它,但我想在java中动态更改它。 有人知道怎么做吗?
使用appcompat-v7:23.0.1下一个代码为我工作:
int color = Color.parseColor("#FFEEEE00"); final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP); for (int i = 0; i < toolbar.getChildCount(); i++) { final View v = toolbar.getChildAt(i); if (v instanceof ImageButton) { ((ImageButton) v).setColorFilter(colorFilter); } }
在public boolean onCreateOptionsMenu(Menu menu)
使用它
您可以使用新的DrawableCompat
类的setTint
(来自support v4 lib)
// Get the icon you want as a drawable Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_menu, null); // "Enable" tinting process drawable = DrawableCompat.wrap(drawable); // Tint it DrawableCompat.setTint(drawable, Color.BLACK); // Set it as action bar icon actionBar.setHomeAsUpIndicator(drawable);
有关可绘制着色的更多详细信息,请参阅Chris Bane关于支持lib V22.1的post