更改Chromecast投射图标颜色

时间:2014-06-19 18:13:06

标签: android chromecast

我使用Google的方法将Cast按钮添加到我的应用中,但它默认为白色图标,在我的白色菜单栏上看不见。如何将Cast图标的颜色更改为黑色?

8 个答案:

答案 0 :(得分:6)

我刚才扩展了MediaRouteActionProvider:

public class ThemeableMediaRouteActionProvider extends MediaRouteActionProvider {
   public ThemeableMediaRouteActionProvider(Context context) {
       super(context);
   }

   @Override
   public MediaRouteButton onCreateMediaRouteButton() {
       MediaRouteButton button = super.onCreateMediaRouteButton();
       colorWorkaroundForCastIcon(button);
       return button;
   }

   @Nullable
   @Override
   public MediaRouteButton getMediaRouteButton() {
      MediaRouteButton button = super.getMediaRouteButton();
      colorWorkaroundForCastIcon(button);
      return button;
   }

   private void colorWorkaroundForCastIcon(MediaRouteButton button) {
      if (button == null) return;
      Context castContext = new ContextThemeWrapper(getContext(), android.support.v7.mediarouter.R.style.Theme_MediaRouter);

      TypedArray a = castContext.obtainStyledAttributes(null,
           android.support.v7.mediarouter.R.styleable.MediaRouteButton, android.support.v7.mediarouter.R.attr.mediaRouteButtonStyle, 0);
      Drawable drawable = a.getDrawable(android.support.v7.mediarouter.R.styleable.MediaRouteButton_externalRouteEnabledDrawable);
      a.recycle();
      DrawableCompat.setTint(drawable, getContext().getResources().getColor(R.color.primary));
      drawable.setState(button.getDrawableState());
      button.setRemoteIndicatorDrawable(drawable);
   }
}

R.color.primary是我想要的颜色。

然后将菜单中的actionProviderClass从MediaRouteActionProvider替换为:

<item
    android:id="@+id/media_route_menu_item"
    android:title="@string/media_route_menu_title"
    bwq:actionProviderClass="tv.test.playback.chromecast.ThemeableMediaRouteActionProvider"
    bwq:showAsAction="always">
</item>

答案 1 :(得分:2)

我扩展了MediaRouteButton。见下面的代码。然后我可以调用applyTint()并将颜色应用于RemoteIndicatorDrawable的所有状态

public class ColorableMediaRouteButton extends MediaRouteButton {

    protected Drawable mRemoteIndicatorDrawable;

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

    public ColorableMediaRouteButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    @Override
    public void setRemoteIndicatorDrawable(Drawable d) {
        mRemoteIndicatorDrawable = d;
        super.setRemoteIndicatorDrawable(d);
    }

    public void applyTint(int color) {
        Drawable wrapDrawable = DrawableCompat.wrap(mRemoteIndicatorDrawable);
        DrawableCompat.setTint(wrapDrawable, color);
    }
}

答案 2 :(得分:1)

如果只是由于风格(黑暗与光线),如果风格被正确定义(即在暗与光的方面扩展右主题),你应该没问题。对于一般样式,请查看此post,如果这不能解决您的问题,请回来告诉我们。

答案 3 :(得分:1)

我自己遇到过这个问题,并通过将pip --versionandroid:theme值更改为app:popuptheme来解决此问题。

请参阅下面的示例代码:

@style/ThemeOverlay.AppCompat.Dark.ActionBar

答案 4 :(得分:1)

如果视图的投射图标只有一种颜色,请为片段或活动使用主题。

<item name="mediaRouteButtonTint">@color/red</item>

如果要通过编程方式设置颜色,请使用以下代码。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_item"
        android:title="@string/media_route_menu_title"
        app:actionProviderClass="androidx.mediarouter.app.MediaRouteActionProvider" />
</menu>
import androidx.core.content.ContextCompat
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.graphics.drawable.DrawableCompat

...

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater?) {
    val tintColor = ContextCompat.getColor(context, R.color.my_color)
    val item = menu.findItem(R.id.menu_item)
    val button = item.actionView
    val castContext = ContextThemeWrapper(context, androidx.mediarouter.R.style.Theme_MediaRouter)
    val attrs = castContext.obtainStyledAttributes(null, androidx.mediarouter.R.styleable.MediaRouteButton, androidx.mediarouter.R.attr.mediaRouteButtonStyle, 0)
    val drawable = attrs.getDrawable(androidx.mediarouter.R.styleable.MediaRouteButton_externalRouteEnabledDrawable)
    attrs.recycle()
    DrawableCompat.setTint(drawable, tintColor)
    drawable.state = button.getDrawableState()
    button.setRemoteIndicatorDrawable(drawable)

答案 5 :(得分:0)

绝对简单回答:

https://developers.google.com/cast/docs/android_sender_advanced#customize_cast_button

只需将 mediaRouteTheme 从该链接复制到您应用的主题,然后将两个样式复制到styles.xml。

答案 6 :(得分:0)

官方答案在这里:

https://developers.google.com/cast/docs/android_sender/customize_ui#customize_cast_button

  

自定义投射按钮

     

要将自定义mediaRouteTheme添加到您应用的主题中:

<style name="Theme.CastVideosTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <!-- ... -->
  <item name="mediaRouteTheme">@style/CustomMediaRouterTheme</item>
</style>
     

声明您的自定义媒体路由器主题并声明一个自定义   mediaRouteButtonStyle

<style name="CustomMediaRouterTheme" parent="Theme.MediaRouter">
  <item name="mediaRouteButtonStyle">@style/CustomMediaRouteButtonStyle</item>
</style>

<style name="CustomMediaRouteButtonStyle" parent="Widget.MediaRouter.Light.MediaRouteButton">
  <item name="mediaRouteButtonTint">#EEFF41</item>
</style>
     如果支持库版本,则应使用

mediaRouteButtonTint   比26.0.0更新。对于较早的支持库版本,请使用   buttonTint

答案 7 :(得分:0)

如果以上解决方案均无效,请尝试此处说明的内容。您可能需要使用colorFilter属性而不是色调。

  public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.some_menu, menu);
    setColorForCast(menu);
  }

  private void setColorForCast(@NonNull Menu menu) {
    MediaRouteButton castButton = (MediaRouteButton) menu.findItem(R.id.cast_button).getActionView();
    if (castButton != null) {
      Drawable drawable = getResources().getDrawable(R.drawable.chromecast);
      int color = ContextCompat.getColor(getContext(), R.color.colorPrimary);
      drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
      drawable.setState(castButton.getDrawableState());
      castButton.setRemoteIndicatorDrawable(drawable);
    }
  }