Android burger/arrow icon dynamic change color

爷,独闯天下 提交于 2019-12-22 09:59:54

问题


I want to change color of burger/arrow icon of navigation drawer. I know I can change it in styles, but I want change it dynamically in java. Did anybody know how to do this?


回答1:


Using appcompat-v7:23.0.1 next code worked for me:

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);
    }
}

Use it in public boolean onCreateOptionsMenu(Menu menu)




回答2:


You can use setTint of the new DrawableCompat class (from 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);

For more details about drawable tinting see Chris Bane post about support lib V22.1



来源:https://stackoverflow.com/questions/32228199/android-burger-arrow-icon-dynamic-change-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!