我在android pie发布之前创建了我的应用程序,在我把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>
Run Code Online (Sandbox Code Playgroud)
我的主要颜色是红色。
Ali*_*yan 104
您可以将其放在onCreate
启动器活动方法的第一行。
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Run Code Online (Sandbox Code Playgroud)
Fau*_*tha 76
使用小米 Note 8 Pro MIUI 12.0.2 Android 10 测试
添加 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
到MainActivity.onCreate();
似乎不起作用
工作解决方案是添加 <item name="android:forceDarkAllowed">false</item>
在我们的主AppTheme
块中styles.xml
例子:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
InputMethodService 更新:
对于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()
不足以防止反转。
小智 48
实际上,这个答案特别适用于React Native开发人员/应用程序:
就像我们所有人都知道的那样,暗模式在Android 10上完全可用
深色主题在 Android 10(API 级别 29)及更高版本中可用
而且这个功能很可能毁了你的应用设计实现,比如 SVG、字体和背景颜色。如果您决定完全禁用force dark mode
,则应遵循以下方法:
将以下代码附加<style>
到styles.xml
文件中的标记内:
<item name="android:forceDarkAllowed">false</item>
Run Code Online (Sandbox Code Playgroud)
注意:文件路径:android/app/src/main/res/values/styles.xml
在第1步之后,你应该有如下内容:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:forceDarkAllowed">false</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
也许上面的stpes没有帮助你但不要担心,就像这篇文章顶部所说的,这个特性是在API级别29上发布的,这意味着你应该将其更改compiledSdkVersion
为29或更高。
要更改compiledSdkVersion
您应该编辑文件中存在compiledSdkVersion
的应用程序 gradle 属性android/app/build.gradle
:
android {
compileSdkVersion 29
Run Code Online (Sandbox Code Playgroud)
也许你会遇到compileSdkVersion rootProject.ext.compileSdkVersion
,别担心,你应该在文件中存在的android gradle 属性中更改它android/build.gradle
:
buildscript {
ext {
buildToolsVersion = "SomeVersion"
minSdkVersion = SomeVersion
compileSdkVersion = 29 // <======= this should be 29 or higher
targetSdkVersion = SomeVersion
Run Code Online (Sandbox Code Playgroud)
提示:为确保您有一个新版本,请使用该rm -rf android/app/build
命令完全删除您的最后一个版本,并在您的模拟器/设备上卸载该应用程序,然后yarn android
再次运行。
Don*_*nix 45
In themes.xml change:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
to
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
Evi*_*n1_ 18
如果你想避免活动的消遣提到,你可以设置标志上的应用程序类在这里
我建议在你的应用程序类中设置它(如果你有一个),如下所示:
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*Pag 17
如果您想在整个应用程序中禁用夜间模式,Ali Rezaiyan 建议的正确方法是继续。
有时您只需要在某些活动或片段中禁用夜间模式(可能是因为遗留的东西)。
所以你可以选择做什么:
禁用整个应用程序:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
禁用单个活动:
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate()
它才能生效小智 14
这对我有用,我从<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>
Run Code Online (Sandbox Code Playgroud)
小智 12
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
将两个主题 DayNight 更改为 Light
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">
小智 10
创建一个继承 Application 类的基础应用程序类,并在覆盖 oncreate 方法中放置以下行:-
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Run Code Online (Sandbox Code Playgroud)
也不要忘记将其添加到 Android 清单中的名称标签下。
不要使用下面的代码。
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Run Code Online (Sandbox Code Playgroud)
这将调用您的活动两次。
只需简单地使用这个
在你的主题文件中
首先将 Style 父级设置为 Light
Theme.MaterialComponents.Light.NoActionBar
Run Code Online (Sandbox Code Playgroud)
然后在你的样式下添加这一行
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
Run Code Online (Sandbox Code Playgroud)
所以你的代码看起来像这样。
<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>
Run Code Online (Sandbox Code Playgroud)
注意:如果您有 android studio 更新版本,请确保您需要使夜间主题的样式相同。Same Color as Light theme
这对我有用(2021 年 4 月)
<item name="android:forceDarkAllowed" tools:targetApi="q"> false </item>
小智 7
在您想要禁用强制黑暗的活动的布局中,将以下属性放置在父布局中:
android:forceDarkAllowed="false"
Run Code Online (Sandbox Code Playgroud)
你的布局应该是这样的:
<?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>
Run Code Online (Sandbox Code Playgroud)
小智 7
只需在您的项目(themes.xml)中修改以下几处更改:
如果您的 Android 工作室有最新版本,请将此应用到“themes.xml”和“themes.xml(night)”。
改变:
<style name="Theme.demo" parent="Theme.MaterialComponents.DayNight.NoActionBar">`
Run Code Online (Sandbox Code Playgroud)
到:
<style name="Theme.demo" parent="Theme.MaterialComponents.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
然后只需添加以下行:
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
Run Code Online (Sandbox Code Playgroud)
只需在每个 ActivityAppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
之后添加即可
super.onCreate(savedInstanceState);
接下来添加
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
您的主题。