如何将Chrome投射菜单项自定义为白色

时间:2018-01-12 06:01:07

标签: java android customization chromecast

我正在尝试将强制转换图标的颜色更改为白色。现在我把它变成了黑色。是否可以仅从样式中更改投射图标的颜色。我使用此链接作为参考:

https://gist.github.com/rharter/c2787f9ddd32651e8885

我使用过这段代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/media_route_menu_item"
        android:title="@string/media_routed"
        android:icon="@mipmap/ic_cast"
        app:actionProviderClass="com.radioapp.utils.ThemeableMediaRouteActionProvider"
        android:actionButtonStyle="@style/MyToolbar"
        app:showAsAction="always"/>

</menu>

我的自定义主题媒体按钮是:

public class ThemeableMediaRouteActionProvider extends MediaRouteActionProvider {

    public ThemeableMediaRouteActionProvider(Context context) {
        super(context);
    }

    @Override public MediaRouteButton onCreateMediaRouteButton() {
        return new ThemeableMediaRouteButton(getContext());
    }
}

// ThemeableMediaRouteButton是:

public class ThemeableMediaRouteButton extends MediaRouteButton {
    private static final String TAG = ThemeableMediaRouteButton.class.getSimpleName();

    private int mMinWidth;
    private int mMinHeight;
    private int mColor;
    private Drawable mRemoteIndicator;

    public ThemeableMediaRouteButton(Context context) {
        this(context, null);
    }

    public ThemeableMediaRouteButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.mediaRouteButtonStyle);
    }

    public ThemeableMediaRouteButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.ThemeableMediaRouteButton, defStyleAttr, 0);
        mColor = a.getColor(R.styleable.ThemeableMediaRouteButton_iconColor, 0);
        setRemoteIndicatorDrawable(a.getDrawable(
                R.styleable.ThemeableMediaRouteButton_routeEnabledDrawable));
        mMinWidth = a.getDimensionPixelSize(
                R.styleable.ThemeableMediaRouteButton_android_minWidth, 0);
        mMinHeight = a.getDimensionPixelSize(
                R.styleable.ThemeableMediaRouteButton_android_minHeight, 0);

        a.recycle();
    }

    @Override protected void drawableStateChanged() {
        super.drawableStateChanged();

        if (mRemoteIndicator != null) {
            int[] myDrawableState = getDrawableState();
            mRemoteIndicator.setState(myDrawableState);
            invalidate();
        }
    }

    public void setRemoteIndicatorDrawable(Drawable d) {
        if (mRemoteIndicator != null) {
            mRemoteIndicator.setCallback(null);
            unscheduleDrawable(mRemoteIndicator);
        }
        mRemoteIndicator = d;
        if (d != null) {
            d.setColorFilter(mColor, PorterDuff.Mode.SRC_ATOP);
            d.setCallback(this);
            d.setState(getDrawableState());
            d.setVisible(getVisibility() == VISIBLE, false);
        }

        refreshDrawableState();
    }

    @Override protected boolean verifyDrawable(Drawable who) {
        return super.verifyDrawable(who) || who == mRemoteIndicator;
    }

    @Override
    public void jumpDrawablesToCurrentState() {
        // We can't call super to handle the background so we do it ourselves.
       // super.jumpDrawablesToCurrentState();
        if (getBackground() != null) {
            DrawableCompat.jumpToCurrentState(getBackground());
        }
        // Handle our own remote indicator.
        if (mRemoteIndicator != null) {
            DrawableCompat.jumpToCurrentState(mRemoteIndicator);
        }
    }

    @Override public void setVisibility(int visibility) {
        super.setVisibility(visibility);

        if (mRemoteIndicator != null) {
            mRemoteIndicator.setVisible(getVisibility() == VISIBLE, false);
        }
    }

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int minWidth = Math.max(mMinWidth,
                mRemoteIndicator != null ? mRemoteIndicator.getIntrinsicWidth() : 0);
        final int minHeight = Math.max(mMinHeight,
                mRemoteIndicator != null ? mRemoteIndicator.getIntrinsicHeight() : 0);
        int width;
        switch (widthMode) {
            case MeasureSpec.EXACTLY:
                width = widthSize;
                break;
            case MeasureSpec.AT_MOST:
                width = Math.min(widthSize, minWidth + getPaddingLeft() + getPaddingRight());
                break;
            default:
            case MeasureSpec.UNSPECIFIED:
                width = minWidth + getPaddingLeft() + getPaddingRight();
                break;
        }
        int height;
        switch (heightMode) {
            case MeasureSpec.EXACTLY:
                height = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                height = Math.min(heightSize, minHeight + getPaddingTop() + getPaddingBottom());
                break;
            default:
            case MeasureSpec.UNSPECIFIED:
                height = minHeight + getPaddingTop() + getPaddingBottom();
                break;
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mRemoteIndicator != null) {
            final int left = getPaddingLeft();
            final int right = getWidth() - getPaddingRight();
            final int top = getPaddingTop();
            final int bottom = getHeight() - getPaddingBottom();

            final int drawWidth = mRemoteIndicator.getIntrinsicWidth();
            final int drawHeight = mRemoteIndicator.getIntrinsicHeight();
            final int drawLeft = left + (right - left - drawWidth) / 2;
            final int drawTop = top + (bottom - top - drawHeight) / 2;

            mRemoteIndicator.setBounds(drawLeft, drawTop,
                    drawLeft + drawWidth, drawTop + drawHeight);
            mRemoteIndicator.draw(canvas);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

查看此SO post。您可以将主题更改为Theme.AppCompat.Light。这个SO post中给出了一个例子。

  

框架会选择正确的(暗或亮)基于   您的操作栏的主题,因此对于具有浅色背景的操作栏,它   会选择一个较暗的图标,反之亦然;