在Android中更改PreferenceFragmentCompat对话框的背景颜色。

huangapple go评论94阅读模式
英文:

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.

在Android中更改PreferenceFragmentCompat对话框的背景颜色。
(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.

在Android中更改PreferenceFragmentCompat对话框的背景颜色。
<sub>(source: pocketnow.com)</sub>

This I've tried:

&lt;item name=&quot;android:windowBackground&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
&lt;item name=&quot;android:colorBackground&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
&lt;item name=&quot;android:colorBackgroundFloating&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;

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>

这就是结果:

在Android中更改PreferenceFragmentCompat对话框的背景颜色。

奖励提示:如果您还想设置 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:

&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.DayNight.NoActionBar&quot;&gt;
    &lt;item name=&quot;alertDialogTheme&quot;&gt;@style/AlertDialogCustom&lt;/item&gt;
&lt;/style&gt;

And then you setup your dialog theme like:

&lt;style name=&quot;AlertDialogCustom&quot; parent=&quot;Theme.AppCompat.Light.Dialog&quot;&gt;
    &lt;item name=&quot;colorAccent&quot;&gt;#FFC107&lt;/item&gt;
    &lt;item name=&quot;android:textColorPrimary&quot;&gt;#FFFFFF&lt;/item&gt;
    &lt;item name=&quot;android:background&quot;&gt;#4CAF50&lt;/item&gt;
&lt;/style&gt;

And this is the result:

在Android中更改PreferenceFragmentCompat对话框的背景颜色。

> Bonus tip: If you want to also setup the default theme for
> MaterialAlertDialogBuilder you must change de attribute
> materialAlertDialogTheme

huangapple
  • 本文由 发表于 2020年8月11日 07:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63349209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定