How to set icon color of MenuItem?

后端 未结 9 704
面向向阳花
面向向阳花 2021-01-30 12:37

I defined a menu item that has ShareActionProvider and share white icon like so :



        
9条回答
  •  终归单人心
    2021-01-30 13:10

    The icon is actually provided by the ShareActionProvider and you can't change it afaik. You can, however, customize the color by setting the textColorPrimary in your styles.xml:

    
    

    
    

    For any custom icons, you would have to color them yourself, ie.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
    
        for(int i = 0; i < menu.size(); i++){
            Drawable drawable = menu.getItem(i).getIcon();
            if(drawable != null) {
                drawable.mutate();
                drawable.setColorFilter(getResources().getColor(R.color.textColorPrimary), PorterDuff.Mode.SRC_ATOP);
            }
        }
    
        return true;
    }
    

提交回复
热议问题