![](/img/trans.png)
[英]How to write preference values with androidx.preference:preference:1.1.1 in kotlin?
[英]Adjust androidx.preference dialogs to follow Material You
我有两个包含首选项部分的应用程序,并且我正在使用首选项库,这是撰写本文时可用的最新版本:
implementation "androidx.preference:preference:1.2.0-rc01"
现在,我将我的应用程序设置为使用并遵循新的 Material3 (或Material You )主题,但我面临的一个问题是,虽然正常对话框的主题正确,但首选项部分中的对话框只是部分主题(角半径)。 他们显然不使用新的 styles,这种不一致正在杀死我:D
目前,如果没有奇怪的解决方法,我没有找到一种方法来为它们设置主题,所以我会很感激你的建议。 这是我目前用于对话的 styles
<!-- Dialog theme -->
<style name="ThemeOverlay.App.MaterialAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="colorOnSurface">?colorAccent</item>
<item name="alertDialogStyle">@style/MaterialAlertDialog.App</item>
<item name="dialogCornerRadius">@dimen/bottom_sheet_corners</item>
</style>
<!-- Note: shape appearance doesn't work with the preference dialogs (they're not material) -->
<style name="MaterialAlertDialog.App" parent="MaterialAlertDialog.Material3">
<item name="shapeAppearance">@style/ShapeAppearance.App.MediumComponent</item>
<item name="shapeAppearanceOverlay">@null</item>
</style>
也许这只是等待的问题?
目前,Preference Dialogs 仍然使用AlertDialog
来膨胀视图。 这是我在styles.xml
中定义的,以将 Material3 对话框主题应用于它们:
注意:这涵盖了大部分 MaterialAlertDialog 样式,但您仍需要定义其他样式,例如角半径和背景/文本 colors。
<item name="alertDialogTheme">@style/ThemeOverlay.Material3.MaterialAlertDialog</item>
<item name="dialogCornerRadius">28dp</item>
经过进一步调查,我发现,截至今天,正确设置首选项对话框主题的唯一方法是使用向@Patrick 提出的解决方案的轻微变化:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="?attr/colorSurface" />
<corners
android:bottomLeftRadius="@dimen/yourcorners"
android:bottomRightRadius="@dimen/yourcorners"
android:topLeftRadius="@dimen/yourcorners"
android:topRightRadius="@dimen/yourcorners" />
</shape>
</item>
<item>
<shape>
<solid android:color="@color/m3_popupmenu_overlay_color" />
<corners
android:bottomLeftRadius="@dimen/yourcorners"
android:bottomRightRadius="@dimen/yourcorners"
android:topLeftRadius="@dimen/yourcorners"
android:topRightRadius="@dimen/yourcorners" />
</shape>
</item>
</layer-list>
当然,请确保以尺寸或直接在文件中定义自定义边框半径。 忽略警告。 没有别的办法。
<!-- Preference dialog theme -->
<style name="ThemeOverlay.App.MaterialAlertDialog.Monet" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="alertDialogStyle">@style/MaterialAlertDialog.App</item>
<item name="dialogCornerRadius">@dimen/yourcorners</item>
<item name="android:background">@drawable/dialog_bg_monet</item>
</style>
<item name="materialAlertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog.Monet</item>
<item name="alertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog.Monet</item>
结果:
上面截图中的应用是开源的,你可以在这里看到完整的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.