【问题标题】:Can't disable Night Mode in my application even with values-night即使使用 values-night 也无法在我的应用程序中禁用夜间模式
【发布时间】:2020-12-26 02:23:20
【问题描述】:

问题是,当我在软件上查看预览时,一切都很好,但是当我在手机上运行该应用程序时,到处都使用白色,它变暗(可绘制对象和背景)。我没有在drawables上使用alpha,只有android:color="@color/white"

问题的根源是我的手机启用了夜间模式,所以它会自动更改应用程序中的颜色。

为了尝试禁用夜间模式,我创建了一个文件夹 values-night 并复制了我的 @styles 和 @colors 以匹配白天和夜间模式。

styles.xml(夜晚)

<resources>

    <!-- Base application theme. -->
        <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowBackground">@color/colorAccent</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

colors.xml(夜晚)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#1d1d1d</color>
    <color name="colorPrimaryDark">#1d1d1d</color>
    <color name="colorAccent">#F8F8F8</color>
    <color name="textColor">#1d1d1d</color>
</resources>

在我的 MainActivity.xml 中

<android.support.constraint.ConstraintLayout
    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:orientation="vertical"
    android:background="@color/colorAccent"
    android:theme="@style/MyTheme"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

在我的 Manifest.xml 中

 android:theme="@style/MyTheme"

我检查了How to disable night mode in my application even if night mode is enable in android 9.0 (pie)?Dark Theme in android 9.0 changes my app layouts ugly 上的答案,并做了values-night 文件夹,但它没有解决问题。

但是问题仍然存在,知道我在这里缺少什么吗?提前致谢。

【问题讨论】:

    标签: java android xml android-studio themes


    【解决方案1】:

    第 1 步: 从您的值文件夹中删除 themes(night).xml

    第 2 步: 更改 themes.xml 中应用样式的父属性

    <style name="Theme.AppName" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    

    <style name="Theme.AppName" parent="Theme.MaterialComponents.Light.NoActionBar">
    

    注意从“DayNight”到“Light”的变化

    现在,即使您的手机设置为暗模式,由于不存在夜间值并且您正在继承您选择的任何 Material Theme 的轻版本,您的应用的主题应该保持不变。

    这对我有用。让我知道它是否也适合你。

    【讨论】:

      【解决方案2】:

      只需删除主题之夜文件,仅此而已,对我有用

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案3】:

      我在小米红米 Note 7 上遇到了同样的问题。我在 MainActivity.create() 中尝试了这段代码:

      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
      

      但它不适用于包括我在内的一些小米设备,但它适用于其他手机。

      所以我找到的唯一解决方案是将&lt;item name="android:forceDarkAllowed" tools:targetApi="q"&gt;false&lt;/item&gt; 添加到styles.xml 中的每个AppTheme。像这样:

      <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
      </style>
      

      它在我的手机上运行良好。

      【讨论】:

      • 这个问题只发生在 Redmi 手机中,但在我的情况下,我只是将它添加到主题中,它的工作就像一个魅力。谢谢 false
      【解决方案4】:

      简单地改变这个

      <style name="YourThemeName" parent="Theme.MaterialComponents.DarkActionBar">
      

      <style name="YourThemeName" parent="Theme.AppCompat.Light.DarkActionBar">
      

      在主题和主题之夜

      并确保主题和主题之夜的所有颜色都相同

      【讨论】:

      • 甚至不是一个可行的解决方案。请用适当的解释更改您的答案。谢谢
      【解决方案5】:

      只需删除themes.xml(night) xml文件 或将Theme.AppCompat.Light.NoActionBar 添加到themes.xml(night) xml 文件中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-02
        • 1970-01-01
        • 2013-07-14
        • 1970-01-01
        • 1970-01-01
        • 2021-12-01
        • 2020-03-18
        • 1970-01-01
        相关资源
        最近更新 更多