【问题标题】:Is there any way to theme a preference dialog?有没有办法给偏好对话框设置主题?
【发布时间】:2020-09-13 06:11:44
【问题描述】:

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

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

主应用主题:

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

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

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

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

有什么建议吗?

【问题讨论】:

    标签: android android-alertdialog material-components-android


    【解决方案1】:

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

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

    【讨论】:

    • 是的,现在在我的应用程序中使用同样的技巧。效果很好。谢谢!!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多