我正在使用 .NET 8 开发一个针对 Android 的 MAUI 应用程序。
一切都与我的MainActivity.cs中的默认主题@style/Maui.SplashTheme配合良好。
但是我需要编辑主题并添加一些配置,因为我的DatePicker的对话框不能很好地显示对话框按钮(确定/取消):
以下this答案我创建了自己的style.xml并设置了我的颜色值,如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyTheme" parent="MainTheme.Base">
<item name="android:datePickerDialogTheme">@style/CustomDatePickerDialog</item>
</style>
<style name="CustomDatePickerDialog" parent="ThemeOverlay.AppCompat.Dialog">
<!--header background-->
<item name="colorAccent">#ff0000</item>
<!--header textcolor-->
<item name="android:textColorPrimaryInverse">#00ff00</item>
<!--body background-->
<item name="android:windowBackground">#0000ff</item>
<!--selected day-->
<item name="android:colorControlActivated">#ff1111</item>
<!--days of the month-->
<item name="android:textColorPrimary">#ffffff</item>
<!--days of the week-->
<item name="android:textColorSecondary">#33ff33</item>
<!--cancel&ok-->
<item name="android:textColor">#00ffff</item>
</style>
</resources>
现在,在我的 MainActivity 中,如果我替换...
Theme = "@style/Maui.SplashTheme"
与:
Theme = "@style/MyTheme"
...我可以解决我的问题并更改对话框的每种颜色。
但现在我的应用程序中存在其他问题:我没有启动屏幕(我想要它),而且 Shell Flyout 也无法按预期工作。 我还阅读了here,我应该在 MainActivity.cs 中保留默认主题。
所以,我的问题是:是否可以用自定义主题(可能在我的 style.xml 文件中)以任何方式覆盖 @style/Maui.SplashTheme 主题? 这样我就可以保留 SplashTheme 的所有内容。
我期待这样的东西(看看parent =“Maui.SplashTheme”),但它不起作用(我没有收到错误或警告,但主题根本不起作用):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="Maui.SplashTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:datePickerDialogTheme">@style/CustomDatePickerDialog</item>
</style>
<style name="CustomDatePickerDialog" parent="ThemeOverlay.AppCompat.Dialog">
<!--header background-->
<item name="colorAccent">#ff0000</item>
<!--header textcolor-->
<item name="android:textColorPrimaryInverse">#00ff00</item>
<!--body background-->
<item name="android:windowBackground">#0000ff</item>
<!--selected day-->
<item name="android:colorControlActivated">#ff1111</item>
<!--days of the month-->
<item name="android:textColorPrimary">#ffffff</item>
<!--days of the week-->
<item name="android:textColorSecondary">#33ff33</item>
<!--cancel&ok-->
<item name="android:textColor">#00ffff</item>
</style>
</resources>
Android 上新的 .NET MAUI 应用程序的基本主题是“Maui.MainTheme”。更新你的 styles.xml 如下所示:
<style name="Maui.MainTheme" parent="Theme.MaterialComponents.DayNight">
<item name="android:windowIsTranslucent">true</item>
<item name="android:datePickerDialogTheme">@style/CustomDatePickerDialog</item>
</style>
<style name="CustomDatePickerDialog" parent="ThemeOverlay.AppCompat.Dialog">
<!--header background-->
<item name="colorAccent">#ff0000</item>
<!--header textcolor-->
<item name="android:textColorPrimaryInverse">#00ff00</item>
<!--body background-->
<item name="android:windowBackground">#0000ff</item>
<!--selected day-->
<item name="android:colorControlActivated">#ff1111</item>
<!--days of the month-->
<item name="android:textColorPrimary">#ffffff</item>
<!--days of the week-->
<item name="android:textColorSecondary">#33ff33</item>
<!--cancel&ok-->
<item name="android:textColor">#00ffff</item>
</style>
您的活动应使用默认的启动主题:
Theme = "@style/Maui.SplashTheme"