[英]Android burger/arrow icon dynamic change color
我想改變導航抽屜的漢堡/箭頭圖標的顏色。 我知道我可以在樣式中更改它,但我想在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的帖子
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.