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

问题描述

我在应用程序中使用自定义主题作为对话框。 自定义主题适用于警报对话框。 但是该主题不适用于诸如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>

MaterialDialog样式:

<!-- 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>

以下是一个主题主题的对话框:

enter image description here

这是一个没有设置主题的偏好对话框(注意角落):

enter image description here

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

有什么建议吗?

解决方法

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

<item name="alertDialogTheme">@style/AppTheme.AlertDialogTheme</item>

我的基本风格和

<style name="AppTheme.AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="dialogCornerRadius">20dp</item>
</style>

作为一种风格。遗憾的是,这使我的整个 AlertDialog.Builder 到 MaterialAlertDialogBu​​ilder 的迁移在某种程度上毫无意义(至少在cornerFamily 被四舍五入的情况下),希望我们将来会看到偏好和材料主题之间更好的整合。

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

,

DialogPreference在后台使用了AlertDialog,而不是MaterialAlertDialog,因此不支持诸如shapeAppearance之类的属性。

作为一种解决方法,您可以在xml文件中使用简单的首选项,并添加一个显示MaterialAlertDialog的OnClickListener。在MaterialAlertDialogBu​​ilder的构造函数中,您可以设置ThemeOverlay:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

 <Preference
    app:icon="..."
    android:key="key_dialog"
    android:title="..."
    />
</PreferenceScreen>

class SettingsFragment:PreferenceFragmentCompat() {

   override fun onCreatePreferences(savedInstanceState: Bundle?,rootKey: String?) {
    addPreferencesFromResource(R.xml.preferences)

    findPreference<Preference>("key_dialog")?.apply {

        onPreferenceClickListener = Preference.OnPreferenceClickListener {
            val items = arrayOf("item_1","item_2","item_3")

            MaterialAlertDialogBuilder(requireContext(),R.style.ThemeOverlay_Settings_Dialog)
                .setTitle(title)
                .setNegativeButton(R.string.button_cancel) { dialog,_ -> dialog.dismiss() }
                .setSingleChoiceItems(items,1) { dialog,which ->
                    // handle onClick
                    dialog.dismiss()
                }
                .show()

            true
        }
    }
}
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...