在 Xamarin Android 中设置布局背景 - 覆盖暗模式

Setting background of layout in Xamarin Android - overriding Dark Mode

我正在尝试为我的 Android 应用程序设置背景。

但是设置为android:background 属性 不起作用:

<GridLayout ...
    android:background="#ffffff00">

我什至在父元素上设置了背景:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff00">

但它仍然只显示深色背景。

我的手机(如果重要的话是小米)开启了深色模式。当我关闭此模式时,背景会按预期工作。

所以我想知道如何在应用中覆盖深色模式?

看起来这个黑暗模式已经覆盖了这个 属性 但这是不可能的。

您是如何让您的应用支持深色模式的?

我遵循了 docs ,只是将应用程序的主题(通常在 res/values/styles.xml 中找到)设置为继承自 DayNight 主题:

<style name="AppTheme" parent="Theme.AppCompat.DayNight">

轻模式

深色模式

相同的代码在我这边按预期工作。(黄色部分是 GridLayout)


建议:

  1. 在空白项目中测试。
  2. 在另一台设备上测试。

根据@Colex-MSFT 的回答我已经解决了:

他的建议是对的,我应该把

<style name="AppTheme" parent="Theme.AppCompat.DayNight">

res/values/styles.xml) 中。然后我再次检查它,发现该项目是使用文件中的以下部分自动创建的:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
         Customize your theme here. 
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

(这些颜色只是在 colors.xml 文件中定义)所以,我只是注释掉了这个(可能会删除)并留下建议行并且它有效 - 我的应用程序不再强制使用暗模式: )