单击 MediaRouteButton 后的样式对话框

Style dialog after click on MediaRouteButton

您好,有人可以帮我处理单击 MediaRouteButton 后出现的样式对话框吗?

灰底白字,不好看。

android.support.v7.app.MediaRouteButton

用样式包裹在父级中

<FrameLayout
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

(我知道我应该在这里使用工具栏,但它不符合我的要求)

在变白的 MediaRouteButton 上效果很好,但对对话框样式没有影响。

我试图查看 Google 提供的示例应用程序,但没有找到任何对我有帮助的东西。 Link to sample app styles

我当前的主题:

  <style name="Theme.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>//Blue
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>//dark blue
    <item name="colorAccent">@color/colorPrimary</item>//blue
</style>

找到适合我的解决方案。

首先你必须在你的 MediaRouteButton 上设置一个自定义的 MediaRouteDialogFactory

mMediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(this, menu, R.id.media_route_menu_item);
MediaRouteButton mediaRouteButton = (MediaRouteButton) mMediaRouteMenuItem.getActionView()
mediaRouteButton.setDialogFactory(new ThemeableMediaRouteDialogFactory());

因为默认的 MediaRouteDialogFactory 将始终创建非主题对话框

@NonNull
public MediaRouteControllerDialogFragment onCreateControllerDialogFragment() {
    return new MediaRouteControllerDialogFragment();
}

这将导致

public MediaRouteControllerDialog onCreateControllerDialog(
        Context context, Bundle savedInstanceState) {
    return new MediaRouteControllerDialog(context);
}

但还有一个主题构造函数MediaRouteControllerDialog(Context context, int theme) 这不是从原始 MediaRouteDialogFactory 调用的。

所以你的 ThemeableMediaRouteDialogFactory 应该是这样的

public class ThemeableMediaRouteDialogFactory extends MediaRouteDialogFactory {
@NonNull
@Override
public MediaRouteChooserDialogFragment onCreateChooserDialogFragment() {
    return new ThemeableMediaRouterChooserDialogFragment();
}

@NonNull
@Override
public MediaRouteControllerDialogFragment onCreateControllerDialogFragment() {
return new ThemeableMediaRouteControllerDialogFragment();
}
}

public class ThemeableMediaRouterChooserDialogFragment extends MediaRouteChooserDialogFragment {

@Override
public MediaRouteChooserDialog onCreateChooserDialog(Context context, Bundle savedInstanceState) {
    return new MediaRouteChooserDialog(context, R.style.CastChooserDialogTheme);
}
}

public class ThemeableMediaRouteControllerDialogFragment extends MediaRouteControllerDialogFragment {

@Override
public MediaRouteControllerDialog onCreateControllerDialog(Context context, Bundle savedInstanceState) {
    return  new MediaRouteControllerDialog(context, R.style.CastControllerDialogTheme);
}
}

你的themes/styles也可以自定义

<style name="DarkDialogTheme" parent="Theme.AppCompat.Dialog">
    <item name="colorPrimary">@color/charcoal_grey</item>
    <item name="colorPrimaryDark">@color/charcoal_grey_dark</item>
    <item name="colorAccent">@color/pumpkin_orange</item>
    <item name="android:windowBackground">@color/dark_grey</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:windowNoTitle">true</item>
</style>

<style name="CastChooserDialogTheme" parent="DarkDialogTheme">
    <item name="android:windowNoTitle">false</item>
    <item name="mediaRouteChooserPrimaryTextStyle">@style/MediaRouteChooserPrimaryText</item>
    <item name="mediaRouteChooserSecondaryTextStyle">@style/MediaRouteChooserSecondaryText</item>
</style>

<style name="CastControllerDialogTheme" parent="DarkDialogTheme">
    <item name="MediaRouteControllerWindowBackground">@color/dark_grey</item>
    <item name="colorPrimary">@color/dark_grey</item>
    <item name="mediaRouteCloseDrawable">@drawable/ic_dialog_close_dark</item>
    <item name="mediaRouteControllerTitleTextStyle">@style/Widget.MediaRouter.ControllerText.Title.Dark</item>
</style>