如何在我的应用程序中禁用夜间模式,即使在 android 9.0(pie)中启用了夜间模式?

我在 android 派发布之前就创建了我的应用程序,在每个布局中我都把 android: background = "white"放在每个设备上,它都能正常工作,但是当我哥哥安装了这个应用程序并启用了 night mode时,我的应用程序一动就变成了一场灾难,一切都变成了黑白电影。

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/COLOR_PRIMARY</item>
<item name="colorPrimaryDark">@color/COLOR_PRIMARY</item>
<item name="colorAccent">@color/COLOR_PRIMARY</item>
<item name="cardViewStyle">@style/CardView</item>
<item name="android:fontFamily">@font/helvetica</item>
</style>

我的原色是红色。

149280 次浏览

您可以将它放在启动程序活动的 onCreate方法的第一行。

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

如果你想在应用程序中禁用夜间模式,那么阿里 · 雷扎伊扬建议的方法是正确的。

有时,您只需要禁用某些活动或片段的夜间模式(可能是因为遗留的东西)。

所以你可以选择做什么:

  • 禁用所有应用程序:

    • AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
  • 禁用单一活动:

    • getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    • 在调用这个命令之后,您可能需要调用 recreate()来使其生效

如果您想要避免活动重新创建,您可以在 Application 类上设置标志,就像前面提到的 给你一样

我建议在你的应用程序类中设置它(如果你有的话) ,如下所示:

public class MyApplication extends Application {
    

public void onCreate() {
super.onCreate();
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}

使用此作为父级禁用黑暗模式 原文: “ Theme. AppCompat. Light.NoActionBar

在你的活动布局中,如果你想禁用强制暗,在父布局中放置以下属性:

android:forceDarkAllowed="false"

你的布局应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_height="match_parent"
android:forceDarkAllowed="false">


<!-- ..... -->


</androidx.constraintlayout.widget.ConstraintLayout>

本机反应,离子(科尔多瓦,电容器)

实际上,这个答案特别适用于 本土反应开发者/应用程序:

就像我们所有人都知道的,黑暗模式在 Android 10上是完全可用的

黑暗主题可以在 Android 10(API 级别29)或更高版本中使用

而且这个特性很可能破坏了应用程序的设计实现,比如 SVG、字体和背景颜色。如果你决定完全禁用 force dark mode,你应该遵循以下方法:

  1. styles.xml文件的 <style>标记中附加 以下代码:

    <item name="android:forceDarkAllowed">false</item>
    

    注释 : 文件路径: android/app/src/main/res/values/styles.xml

  2. 在步骤 1之后,应该有如下内容:

    <resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="android:forceDarkAllowed">false</item>
    </style>
    </resources>
    
  3. 也许上面的步骤对你没有帮助,但是不要担心,就像在这篇文章的顶部说的,这个功能是在 空气污染指数29上发布的,这意味着你应该把 compileSdkVersion改成 29或者更高。

  4. 要更改 compileSdkVersion,您应该编辑 android/app/build.gradle文件中存在的 App gradle属性中的 compileSdkVersion:

    android {
    compileSdkVersion 29
    
  5. 也许 你面对的是 compileSdkVersion rootProject.ext.compileSdkVersion,不要担心你应该改变这个在 android/build.gradle文件中存在的 机器人级别属性:

    buildscript {
    ext {
    buildToolsVersion = "SomeVersion"
    minSdkVersion = SomeVersion
    compileSdkVersion = 29 // <======= this should be 29 or higher
    targetSdkVersion = SomeVersion
    

提示 : 为了确保有一个新的版本,请使用 rm -rf android/app/build命令完全删除上一个版本,并在模拟器/设备上卸载应用程序,然后再次运行 yarn android

在 themes.xml 中更改:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">

使用 小米 Note 8 Pro MIUI 12.0.2 Android 10进行测试

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);添加到 MainActivity.onCreate();似乎不起作用

工作解决方案是在 styles.xml中的主 AppTheme块中添加 <item name="android:forceDarkAllowed">false</item>

例如:

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

更新输入法服务:

对于 InputMethodService/也许如果我们在任何 Service充气布局
我发现在 Dark Mode - Xiaomi MIUI 12.0.3中有一个黑色的颜色被倒转成了白色。
为了防止倒置,所以黑色仍然会是黑色,一定要在 InputMethodService中设置一个主题。

onCreate(),在 super.onCreate()调用这些方法之前

  • getApplication().setTheme()
  • setTheme()

告诉你,我正在通过 Light Theme (Theme.MaterialComponents.Light.NoActionBar),它有 android:forceDarkAllowed=false
在我的情况下,对于 InputMethodService,仅仅调用 getApplication().setTheme()是不足以防止倒置的。

只要把这个换掉

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

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

无论是主题还是主题之夜

确保所有颜色在主题和主题之夜都是一样的

使用以下行可以帮助您不超载暗模式在您的应用程序。

android:forceDarkAllowed="false"

用法的例子,

<LinearLayout 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:orientation="vertical"
android:background="#FFFFFF"
android:forceDarkAllowed="false"
tools:context=".SplashActivity">

科尔多瓦开发商的另一个选择是:

来自 https://developer.android.com/guide/webapps/dark-theme

“ WebView 不能仅仅根据媒体查询的存在来区分,所以它需要一个带有暗值的配色方案元标签来表明一个网页有自己的暗主题。为了防止双重变暗(即在媒体查询定制之上应用颜色倒置) ,WebView 在这种模式下将@media (偏好颜色方案: 深色)评估为 false。”

因此,我添加了 <meta name="color-scheme" content="dark">到我的 html 和变-它现在检测我的喜好-配色方案: 黑暗的查询,并没有应用自动变暗。

这对我很有效(2021年4月)

  1. 在你的主题下添加这一行:

<item name="android:forceDarkAllowed" tools:targetApi="q"> false </item>

  1. 删除 themes.xml (night)文件(在 value 文件夹下)

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

将两个主题都从昼夜变为光明

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">

除了 Fauzi Danartha 对 IMS 部分的回答。 可以扩展主题

<item name="android:forceDarkAllowed" tools:targetApi="q"> false </item>

不是来自材料设计的主题,而是来自 android:Theme.DeviceDefault.InputMethod(自 targetSdkVersion 14)。

也不需要调用 getApplication().setTheme(),只调用 setTheme()就足够了。

检查 MIUI 12.0.4。

我已经经历了很多解决方案,但没有一个是健壮的,直到我删除了“ value-night”文件夹从“ res”文件夹。

And change use parent theme: <style name="Theme.CryptoKite" parent="Theme.MaterialComponents.Light.DarkActionBar">

在 < br/> < br/> super.onCreate(savedInstanceState);后面加上 < br/> < br/> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); < br/> < br/>

所有活动

接下来在主题中添加 < br/> < br/> <item name="android:forceDarkAllowed" tools:targetApi="q">false</item> < br/> < br/> 。

这行代码对我有用。

<!--To disable dark mode-->
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

这对我有用,我改变了 从 <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"><style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

文件路径: android/app/src/main/res/values/styles.xml

所有代码:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:textColor">#000000</item>
</style>

对我来说,我不得不添加 <item name="android:forceDarkAllowed">false</item>Theme.App.SplashScreen的风格也,我使用裸世博会。

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:textColor">#000000</item>
<item name="android:forceDarkAllowed">false</item>
</style>
<style name="Theme.App.SplashScreen" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Below line is handled by '@expo/configure-splash-screen' command and it's discouraged to modify it manually -->
<item name="android:windowBackground">@drawable/splashscreen</item>
<item name="android:forceDarkAllowed">false</item>
<!-- Customize your splash screen theme here -->
</style>
</resources>


去添加:

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

在你的 主题,即使黑暗模式是在你的设备启用。光的主题是应用在你的应用程序。

只需修改项目中下面的一些更改(themes.xml) :

如果你的 Android 工作室有最新的版本,那么把它应用到“ themes.xml”和“ themes.xml (night)”上。

改变:

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

致:

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

然后添加以下一行:

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

永远不要添加这个代码:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

如果你把它放在你的 onCreate 方法的 MainActivity 中,onCreate 方法将会被调用两次! ! !我花了一个小时才发现,添加这一行导致了问题!

只需在 style.xml 中添加这一行:

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

在我的代码中是这样的:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

改变光线和黑暗的主题为相同的颜色。 即使设备处于黑暗模式,这也不会影响输出。

value/theme.xmlnight/theme.xml内部

更改以下行

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

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

该应用程序将在黑暗模式,但即使在黑暗模式,它将显示颜色的光模式,它将始终光。

我使用的是 kotline

在活动课上

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

在 value-> style-> Themes/style 类中,我添加了以下代码

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:forceDarkAllowed" tools:ignore="NewApi">false</item>
</style

<item name="android:forceDarkAllowed">false</item>添加到 styles.xml文件后,不要忘记将父主题从

Theme.AppCompat.DayNight.NoActionBar

Theme.AppCompat.Light.NoActionBar

从模拟器卸载应用程序,运行 cd android && ./gradlew clean,它应该可以正常工作。

不要使用以下代码。

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

这会调用你的活动两次。

只要用这个

在你的主题文件里

首先将样式设置为“光”父级

Theme.MaterialComponents.Light.NoActionBar

然后在你的样式下添加这条线

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

你的代码是这样的。

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/primary_color_app</item>
<item name="colorPrimaryVariant">@color/status_bar_color</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>


<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>


<!-- Customize your theme here. -->
</style>

注意 : 如果您有 android 工作室更新版本,请确保您需要使样式相同的夜间主题。 Same Color as Light theme

我有这个力量夜间模式的问题,在红米注释7专业。但是,当我使用

SetDefaultNight 模式;

这个方法在我的应用程序级别类和整个应用程序主题被改变为轻的主题,但实际上我希望主题作为夜晚。

我找到了特定观点的解决方案:

Android: forcDark 允许 = “ false”

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white_black"
android:elevation="@dimen/dim4"
android:forceDarkAllowed="false"
android:forceHasOverlappingRendering="false"
app:itemIconTint="@drawable/highlighted_bottom_nav_item"
app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
app:itemTextAppearanceInactive="@style/BottomNavigationView"
app:itemTextColor="@drawable/highlighted_bottom_nav_item"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_nav_menu" />

然后就修好了。

创建一个基本的应用程序类,它继承 Application 类,在覆盖 oncreate 方法内部放置如下行:-

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

也不要忘记在 android 清单中添加这个名称标签下的名称。

在 styles.xml 文件中将 AppTheme 中的 DayNight 更改为 Light。