英文:
Can't disable Night Mode in my application even with values-night
问题
问题是,当我在软件上预览时,一切都很正常,但是当我在手机上运行应用时,到处都使用白色,它会变暗(可绘制和背景)。我在可绘制的部分没有使用alpha,只用了 android:color="@color/white"
。
问题的源头在于我的手机启用了夜间模式,因此它自动在应用中改变了颜色。
为了尝试禁用夜间模式,我创建了一个名为 values-night
的文件夹,并复制了我的 @styles 和 @colors,以匹配白天和夜间模式。
styles.xml (night)
<resources>
<!-- 应用程序的基础主题。 -->
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- 在此自定义您的主题。 -->
<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 (night)
<?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"
我在 https://stackoverflow.com/questions/57175226/how-to-disable-night-mode-in-my-application-even-if-night-mode-is-enable-in-andr/57175501 和 https://stackoverflow.com/questions/56573786/dark-theme-in-android-9-0-changes-my-app-layouts-ugly 上检查了答案,并且创建了 values-night
文件夹,但问题仍然存在。有任何关于我漏掉了什么的想法吗?提前致谢。
英文:
The problem is that when I look at the preview on the software everything is fine, but when I run the app on my phone everywhere is used the color white it turn dark (drawables and backgrounds). I haven't used an alpha on the drawables, only android:color="@color/white"
.
The source of the problem is that my phone had the Night Mode enable, so it automatically changed the colors in the app.
To try to disable the Night Mode I created a folder values-night
and copied my @styles and @colors to match the Day and Night Mode.
styles.xml (night)
<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 (night)
<?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>
In my 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">
In my Manifest.xml
android:theme="@style/MyTheme"
I checked the answers on https://stackoverflow.com/questions/57175226/how-to-disable-night-mode-in-my-application-even-if-night-mode-is-enable-in-andr/57175501 and https://stackoverflow.com/questions/56573786/dark-theme-in-android-9-0-changes-my-app-layouts-ugly and did the values-night
folder, but it didn't resolve the problem.
But the problem persist, any idea what am I missing here? Thanks in advance.
答案1
得分: 20
我在我的小米Redmi Note 7上遇到了相同的问题。我尝试将此代码放在MainActivity.create()
中:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
但是在包括我的设备在内的一些小米手机上不起作用,尽管在其他手机上起作用。
因此,我找到的唯一解决方案是在styles.xml
中的每个AppTheme
中添加<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
,就像这样:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>
这对我的手机有效。
英文:
I had the same issue on my Xiaomi Redmi Note 7. I tried this code inside MainActivity.create()
:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
But it doesn't work on some of Xiaomi devices including mine, though it works on other phones.
So the only solution I've found is to add <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
to each of your AppTheme
in styles.xml
. Like this:
<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>
It worked fine for my phone.
答案2
得分: 3
只删除themes.xml(night)
XML 文件
或在themes.xml(night)
XML 文件中添加Theme.AppCompat.Light.NoActionBar
英文:
Just delete themes.xml(night)
xml file
or add Theme.AppCompat.Light.NoActionBar
to themes.xml(night)
xml file
答案3
得分: 2
Step 1:
从您的 values 文件夹中删除 themes(night).xml
Step 2:
将 themes.xml
中您的应用程序样式的父属性更改为:
<style name="Theme.AppName" parent="Theme.MaterialComponents.Light.NoActionBar">
请注意从 "DayNight" 到 "Light" 的更改。
现在,即使您的手机设置为暗模式,由于没有夜间模式的值存在,并且您继承了您选择的任何 Material 主题的浅色版本,您的应用程序主题应保持不变。
这就是对我起作用的方法。如果对您也起作用,请告诉我。
英文:
Step 1:
Delete themes(night).xml
from your values folder
Step 2:
Change the parent attribute of your app style in themes.xml
from
<style name="Theme.AppName" parent="Theme.MaterialComponents.DayNight.NoActionBar">
to
<style name="Theme.AppName" parent="Theme.MaterialComponents.Light.NoActionBar">
Note the change from "DayNight" to "Light"
Now, even if your phone is set to dark-mode, since no values for night exists and you are inheriting the light version of whatever Material Theme you choose, your app's theme should remain the same.
This is what worked for me. Let me know if it works for you too.
答案4
得分: -1
只需删除themes-night文件,就可以了,对我有效。
英文:
Just delete themes-night file, that's all, works for me
答案5
得分: -1
1..只需从原始颜色中复制主要颜色(Light),然后将其替换为夜间主要颜色。
将这部分代码进行更改:
style name="Theme.FakeWhatsApp" parent="Theme.MaterialComponents.DayNight.NoActionBar"
更改为:
style name="Theme.FakeWhatsApp" parent="Theme.MaterialComponents.Light.NoActionBar"
英文:
1..Just copy your primary color from them(Light) and replace them(night) main color.
change this
style name="Theme.FakeWhatsApp" parent="Theme.MaterialComponents.DayNight.NoActionBar"
to this
style name="Theme.FakeWhatsApp" parent="Theme.MaterialComponents.Light.NoActionBar"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论