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



我想更改导航抽屉的汉堡/箭头图标的颜色。我知道我可以改变它的样式,但我想在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(来自支持v4库)

// 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关于支持库V22.1的帖子

最新更新