有什么方法可以设置首选项对话框的主题吗?

Ami*_*ant 5 android android-alertdialog material-components-android

我在我的应用程序中使用对话框的自定义主题。自定义主题适用于警报对话框。但该主题不适用于 ListPreference、CheckboxPreference 等首选项对话框。

目前我正在使用MaterialAlertDialogTheme属性应用对话框主题,如下所示:

主要应用主题:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:navigationBarColor">?attr/colorPrimary</item>
    <item name="android:windowTranslucentStatus">true</item>

    <!-- Material Dialog theme -->
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog</item>
</style>
Run Code Online (Sandbox Code Playgroud)

材质对话框样式:

<!-- Styles for Material Dialogs -->
<style name="ThemeOverlay.App.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="colorSurface">@android:color/white</item>
    <item name="alertDialogStyle">@style/MaterialAlertDialog.App</item>
    <item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialog.App.Title.Text</item>
    <item name="buttonBarPositiveButtonStyle">@style/Widget.App.PositiveButton</item>
</style>

<style name="MaterialAlertDialog.App" parent="MaterialAlertDialog.MaterialComponents">
    <item name="shapeAppearance">@style/ShapeAppearance.App.MediumComponent</item>
</style>

<style name="MaterialAlertDialog.App.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
    <item name="android:textStyle">bold</item>
</style>

<style name="Widget.App.PositiveButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
    <item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item>
    <item name="android:textAllCaps">false</item>
</style>

<style name="ThemeOverlay.App.Button" parent="">
    <item name="colorPrimary">@android:color/black</item>
</style>

<style name="ShapeAppearance.App.MediumComponent" parent="ShapeAppearance.MaterialComponents.MediumComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
</style>

<style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">4dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这是一个完美主题的对话框:

在此输入图像描述

这是一个没有主题的首选项对话框(注意角落):

在此输入图像描述

我也尝试在主主题中设置alertDialog属性,但它不起作用。

有什么建议么?

Ebr*_*owi 8

我需要解决这个问题的是添加

<item name="alertDialogTheme">@style/AppTheme.AlertDialogTheme</item>
Run Code Online (Sandbox Code Playgroud)

以我的基本风格和

<style name="AppTheme.AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="dialogCornerRadius">20dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

作为一种风格。遗憾的是,这使得我的整个 AlertDialog.Builder 到 MaterialAlertDialogBu​​ilder 的迁移在某种程度上毫无意义(至少在cornerFamily是圆形的情况下),希望我们将来能看到首选项和材料主题之间更好的集成。

参考: https: //www.reddit.com/r/androiddev/comments/dg8jfo/styling_androidx_preference_dialogs_using/