英文:
Change PreferenceFragmentCompat dialog background color in Android
问题
I recently started working with PreferenceFragmentCompat and MaterialComponents and noticed that in Dark mode, Dialogs generated by preferences like ListPreference always have a weird grey android background, regardless of the background attributes I change in the style.
(subsource: pocketnow.com)
This is what I've tried:
<item name="android:windowBackground">@color/colorPrimaryDark</item>
<item name="android:colorBackground">@color/colorPrimaryDark</item>
<item name="android:colorBackgroundFloating">@color/colorPrimaryDark</item>
I also tried using android:background which worked, but it broke everything.
So I investigated a bit more into elements like EditTextPreferenceDialogFragment and found that most of them create and show an AlertDialog instance within the same function of PreferenceDialogFragment without any possibility to change its style.
At least, that's the conclusion I've reached after some research on the subject.
My question is, has anyone found a workaround for this? Am I doing something wrong? I would like to have those dialogs match my app theme, even if it's just the background color.
By the way, I apologize if this question has already been answered before. I searched here, but I couldn't find anything with similar answers or results for different problems. Thank you.
英文:
I recently started working with PreferenceFragmentCompat and MaterialComponents and noticed that in Dark mode, Dialogs generated by preferences like ListPreference always have that weird grey android background no matter how many background attributes I change in the style.
<sub>(source: pocketnow.com)</sub>
This I've tried:
<item name="android:windowBackground">@color/colorPrimaryDark</item>
<item name="android:colorBackground">@color/colorPrimaryDark</item>
<item name="android:colorBackgroundFloating">@color/colorPrimaryDark</item>
I've also tried with android:background and worked but it brokes everything.
So I investigated a bit in-depth at elements like EditTextPreferenceDialogFragment and most of them create and show an AlertDialog instance in the same function of PreferenceDialogFragment without any possibility to change its style.
Or at least that's the conclusion I've come to after some research on the subject.
My question is, has anyone found a workaround for this? Am I doing something wrong? Cause I would like to have those dialogs matching my app theme even if it's just the background color.
Btw, sorry if it has been already answered before. I also searched in here but found nothing and similar answers for different problems without results. Thanks.
答案1
得分: 1
发现了!在更仔细阅读了 AndroidX 包后,发现当在构造函数中没有指定时,AlertDialog.Builder 默认从属性中检索默认主题。您可以在此处查看
因此,解决方案是在活动中为对话框添加一个特定的主题,如下所示:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="alertDialogTheme">@style/AlertDialogCustom</item>
</style>
然后,设置对话框主题如下:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FFC107</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#4CAF50</item>
</style>
这就是结果:
奖励提示:如果您还想设置 MaterialAlertDialogBuilder 的默认主题,您必须更改属性 materialAlertDialogTheme。
英文:
Found it! After reading more the AndroidX package found that by default the AlertDialog.Builder retrieves a default theme from an attribute when no specified in the constructor. You can see it here
So a solution would be to add a specific theme for dialogs in the activity like this:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="alertDialogTheme">@style/AlertDialogCustom</item>
</style>
And then you setup your dialog theme like:
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FFC107</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#4CAF50</item>
</style>
And this is the result:
> Bonus tip: If you want to also setup the default theme for
> MaterialAlertDialogBuilder you must change de attribute
> materialAlertDialogTheme
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论