我正在使用谷歌的方法将Cast按钮添加到我的应用程序,但它默认为白色图标,这对我的白色菜单栏是不可见的。如何将Cast图标的颜色更改为黑色?
如果它只是由于风格(暗与光),你应该没有正确定义风格(即在黑暗与光线方面扩展正确的主题)。对于一般造型,请查看此post,如果这不能解决您的问题,请回来告诉我们。
我刚才扩展了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>
我扩展了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);
}
}
我自己经历过这个问题,并通过将android:theme
和app:popuptheme
值更改为@style/ThemeOverlay.AppCompat.Dark.ActionBar
来解决它。
请参阅以下示例代码:
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#4dffffff"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
绝对简单的答案在这里:
https://developers.google.com/cast/docs/android_sender_advanced#customize_cast_button
因此,只需将mediaRouteTheme从该链接复制到您应用的主题,然后将两个样式复制到styles.xml。