问题描述
我在应用程序中使用自定义主题作为对话框。 自定义主题适用于警报对话框。 但是该主题不适用于诸如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>
我也尝试过在主主题中设置alertDialog属性,但是它不起作用。
有什么建议吗?
解决方法
我需要解决这个问题是添加
<item name="alertDialogTheme">@style/AppTheme.AlertDialogTheme</item>
我的基本风格和
<style name="AppTheme.AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="dialogCornerRadius">20dp</item>
</style>
作为一种风格。遗憾的是,这使我的整个 AlertDialog.Builder 到 MaterialAlertDialogBuilder 的迁移在某种程度上毫无意义(至少在cornerFamily 被四舍五入的情况下),希望我们将来会看到偏好和材料主题之间更好的整合。
参考:https://www.reddit.com/r/androiddev/comments/dg8jfo/styling_androidx_preference_dialogs_using/
, DialogPreference在后台使用了AlertDialog,而不是MaterialAlertDialog,因此不支持诸如shapeAppearance
之类的属性。
作为一种解决方法,您可以在xml文件中使用简单的首选项,并添加一个显示MaterialAlertDialog的OnClickListener。在MaterialAlertDialogBuilder的构造函数中,您可以设置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
}
}
}
}