MenuItem tinting on AppCompat Toolbar

前端 未结 8 1983
轮回少年
轮回少年 2020-11-28 19:26

When I use drawables from the AppCompat library for my Toolbar menu items the tinting works as expected. Like this:



        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 20:00

    After the new Support library v22.1, you can use something similar to this:

      @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_home, menu);
            Drawable drawable = menu.findItem(R.id.action_clear).getIcon();
    
            drawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTint(drawable, ContextCompat.getColor(this,R.color.textColorPrimary));
            menu.findItem(R.id.action_clear).setIcon(drawable);
            return true;
        }
    

提交回复
热议问题