Android汉堡/箭头图标动态改变颜色

时间:2015-08-26 13:36:59

标签: java android navigation-drawer hamburger-menu

我想改变导航抽屉的汉堡/箭头图标的颜色。我知道我可以在样式中更改它,但我想在java中动态更改它。有人知道怎么做吗?

2 个答案:

答案 0 :(得分:10)

使用 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)

中使用它

答案 1 :(得分:0)

您可以使用新setTint类的DrawableCompat(来自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 post about support lib V22.1