为什么Cast MediaRouteButton在Holo.Light Action-Bar上总是白色?

时间:2015-02-07 17:58:26

标签: android android-actionbar android-theme chromecast android-styles

我是从自定义MediaRouteButton移动到操作栏内的一个,但它没有正确显示。自定义为白色的按钮是我想要的。但是,即使动作栏是" Holo.Light"按钮仍然是动作栏上的白色(并且几乎看不见)。样式。按钮应该是黑暗的。

该按钮创建为XML菜单项:

<item
    android:id="@+id/menu_item_media_route"
    android:title="@string/menu_item_media_route"
    android:actionViewClass="android.support.v7.app.MediaRouteButton"
    android:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
    android:showAsAction="always" />

我的应用程序风格&#34; @ style / AppTheme&#34;:

<style name="AppTheme" parent="android:Theme.Holo.Light">
</style>

我的主题活动&#34; @ style / FullscreenActionbarTheme&#34;:

<style name="FullscreenActionbarTheme" parent="android:Theme.Holo.Light">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:actionBarStyle">@style/FullscreenActionbar</item>
</style>

<style name="FullscreenActionbar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
</style>

我没有自定义&#34; ic_media_route_(开|关).png&#34; drawables - 我曾经删除它们。

我尝试过改变各种风格,虽然动作栏会变暗,但是施法按钮总是白色。 (因为它应该在一个黑暗的动作条上,但不是一个轻的动作条。)

按钮功能齐全,颜色错误。 &#34;选择器&#34;当我按下按钮时出现的对话框被设置为&#34; Holo.Light&#34;。

那么为什么我的演员按钮在&#34; Holo.Light&#34;主题好像是一个&#34; Holo&#34; (黑暗)主题?

2 个答案:

答案 0 :(得分:2)

取自:Link

  

警告:实施提供媒体路由器的活动时   接口必须扩展ActionBarActivity或FragmentActivity   来自Android支持库,即使你的android:minSdkVersion   是API 11或更高版本。

ActionBarActivity已被AppCompatActivity取代,因此您应该使用它。

支持-V7 MediaRouteButton取决于此。查看super电话:

public MediaRouteButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(MediaRouterThemeHelper.createThemedContext(context), attrs, defStyleAttr);
    ....
    ....
}

MediaRouterThemeHelper.createThemedContext(Context)

public static Context createThemedContext(Context context) {
    boolean isLightTheme = isLightTheme(context);
    return new ContextThemeWrapper(context, isLightTheme ?
            R.style.Theme_MediaRouter_Light : R.style.Theme_MediaRouter);
}
通过解析isLightTheme&lt;&lt; ==来设置

R.attr.isLightTheme这是一个支持库属性。当您的父主题由框架提供时,它将不存在,如android:Theme.Holo.Light的情况。

private static boolean isLightTheme(Context context) {
    TypedValue value = new TypedValue();
    return context.getTheme().resolveAttribute(R.attr.isLightTheme, value, true)
            && value.data != 0;
}

所以,isLightThemefalse&amp;你得到了MediaRouteButton ==&gt;的黑暗主题版本......总是白。

请注意,警告语句暗示您的父主题必须是AppCompat主题 - AppCompatActivity(或ActionBarActivity)无法与{{1}一起使用}。

修改

在这里进行了很多讨论:Link

可以通过聊天记录阅读所尝试的方法。最后,媒体路由器支持库似乎需要一些工作才能生产就绪。在此处阅读更多内容:MediaRouteActionProvider connection dialog theme

答案 1 :(得分:0)

如果所有其他方法都失败了,您可以在onCreate()中以编程方式更改颜色:

   ImageButton button = ((ImageButton) toolbar.getChildAt( ... )); // The view index of the button
    button.setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);