簡體   English   中英

Android漢堡/箭頭圖標動態改變顏色

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM