android toolbar overflow icon color

3312 ワード

    /**
     * Change android toolbar overflow icon color
     * @param activity:activity
     * @param color:your want to color
     * @link http://stackoverflow.com/questions/26997231/android-lollipop-material-design-overflow-menu-icon-color
     */
    public static void setOverflowButtonColor(final Activity activity, final int color) {
        final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_description);
        final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
        if (decorView==null)return;
        final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
        if (viewTreeObserver==null)return;
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                final ArrayList outViews = new ArrayList();
                decorView.findViewsWithText(
                        outViews,
                        overflowDescription,
                        View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
                if (outViews.isEmpty()) {
                    return;
                }
                try {
                    AppCompatImageView overflow = (AppCompatImageView) outViews.get(0);
                    if (overflow != null) {
                        overflow.setColorFilter(color);
                    }
                    if (Build.VERSION.SDK_INT < 16) {
                        decorView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
                        decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                } catch (Exception e) {
                }
            }
        });
    }

link: http://stackoverflow.com/questions/26997231/android-lollipop-material-design-overflow-menu-icon-color