MenuItem tinting on AppCompat Toolbar

前端 未结 8 1919
轮回少年
轮回少年 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:23

    Here is the solution that I use; you can call it after onPrepareOptionsMenu() or the equivalent place. The reason for mutate() is if you happen to use the icons in more than one location; without the mutate, they will all take on the same tint.

    public class MenuTintUtils {
        public static void tintAllIcons(Menu menu, final int color) {
            for (int i = 0; i < menu.size(); ++i) {
                final MenuItem item = menu.getItem(i);
                tintMenuItemIcon(color, item);
                tintShareIconIfPresent(color, item);
            }
        }
    
        private static void tintMenuItemIcon(int color, MenuItem item) {
            final Drawable drawable = item.getIcon();
            if (drawable != null) {
                final Drawable wrapped = DrawableCompat.wrap(drawable);
                drawable.mutate();
                DrawableCompat.setTint(wrapped, color);
                item.setIcon(drawable);
            }
        }
    
        private static void tintShareIconIfPresent(int color, MenuItem item) {
            if (item.getActionView() != null) {
                final View actionView = item.getActionView();
                final View expandActivitiesButton = actionView.findViewById(R.id.expand_activities_button);
                if (expandActivitiesButton != null) {
                    final ImageView image = (ImageView) expandActivitiesButton.findViewById(R.id.image);
                    if (image != null) {
                        final Drawable drawable = image.getDrawable();
                        final Drawable wrapped = DrawableCompat.wrap(drawable);
                        drawable.mutate();
                        DrawableCompat.setTint(wrapped, color);
                        image.setImageDrawable(drawable);
                    }
                }
            }
        }
    }
    

    This won't take care of the overflow, but for that, you can do this:

    Layout:

    <android.support.v7.widget.Toolbar
        ...
        android:theme="@style/myToolbarTheme" />
    

    Styles:

    <style name="myToolbarTheme">
            <item name="colorControlNormal">#FF0000</item>
    </style>
    

    This works as of appcompat v23.1.0.

    0 讨论(0)
  • 2020-11-28 20:23

    This worked for me:

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    
            val inflater = menuInflater
            inflater.inflate(R.menu.player_menu, menu)
    
            //tinting menu item:
            val typedArray = theme.obtainStyledAttributes(IntArray(1) { android.R.attr.textColorSecondary })
            val textColor = typedArray.getColor(0, 0)
            typedArray.recycle()
    
            val item = menu?.findItem(R.id.action_chapters)
            val icon = item?.icon
    
            icon?.setColorFilter(textColor, PorterDuff.Mode.SRC_IN);
            item?.icon = icon
            return true
        }
    

    Or you can use tint in drawable xml:

    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:tint="?android:textColorSecondary"
        android:viewportWidth="384"
        android:viewportHeight="384">
        <path
            android:fillColor="#FF000000"
    
            android:pathData="M0,277.333h384v42.667h-384z" />
        <path
            android:fillColor="#FF000000"
            android:pathData="M0,170.667h384v42.667h-384z" />
        <path
            android:fillColor="#FF000000"
            android:pathData="M0,64h384v42.667h-384z" />
    </vector>
    
    0 讨论(0)
提交回复
热议问题