无法在我的应用程序中禁用夜间模式,即使使用了“values-night”。

huangapple go评论89阅读模式
英文:

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/57175501https://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=&quot;@color/white&quot;.

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)

&lt;resources&gt;

    &lt;!-- Base application theme. --&gt;
        &lt;style name=&quot;MyTheme&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;
        &lt;!-- Customize your theme here. --&gt;
        &lt;item name=&quot;android:windowBackground&quot;&gt;@color/colorAccent&lt;/item&gt;
        &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
        &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
        &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;
    &lt;/style&gt;

&lt;/resources&gt;

colors.xml (night)

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;resources&gt;
    &lt;color name=&quot;colorPrimary&quot;&gt;#1d1d1d&lt;/color&gt;
    &lt;color name=&quot;colorPrimaryDark&quot;&gt;#1d1d1d&lt;/color&gt;
    &lt;color name=&quot;colorAccent&quot;&gt;#F8F8F8&lt;/color&gt;
    &lt;color name=&quot;textColor&quot;&gt;#1d1d1d&lt;/color&gt;
&lt;/resources&gt;

In my MainActivity.xml

&lt;android.support.constraint.ConstraintLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    android:background=&quot;@color/colorAccent&quot;
    android:theme=&quot;@style/MyTheme&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

In my Manifest.xml

 android:theme=&quot;@style/MyTheme&quot;

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 &lt;item name=&quot;android:forceDarkAllowed&quot; tools:targetApi=&quot;q&quot;&gt;false&lt;/item&gt; to each of your AppTheme in styles.xml. Like this:

&lt;style name=&quot;MyTheme&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;
    &lt;item name=&quot;android:forceDarkAllowed&quot; tools:targetApi=&quot;q&quot;&gt;false&lt;/item&gt;
&lt;/style&gt;

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

&lt;style name=&quot;Theme.AppName&quot; parent=&quot;Theme.MaterialComponents.DayNight.NoActionBar&quot;&gt;

to

&lt;style name=&quot;Theme.AppName&quot; parent=&quot;Theme.MaterialComponents.Light.NoActionBar&quot;&gt;

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=&quot;Theme.FakeWhatsApp&quot; parent=&quot;Theme.MaterialComponents.DayNight.NoActionBar&quot;

to this

style name=&quot;Theme.FakeWhatsApp&quot; parent=&quot;Theme.MaterialComponents.Light.NoActionBar&quot;

huangapple
  • 本文由 发表于 2020年9月8日 00:41:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63781371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定