android - 无法在我的应用程序上禁用系统的暗模式效果 - Android
问题描述
我是 android 新手,我正在尝试禁用系统的暗模式对我的应用程序的影响,但无论如何都无法实现。这是我的 theme.xml,在主题之夜中也是如此
<style name="Theme.MyAppName" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/dark_red</item>
<item name="colorPrimaryVariant">@color/dark_red</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>
<!-- Customize your theme here. -->
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>
我尝试删除主题之夜文件。我也试过这条线突出在 onCreate 下面我也 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
试过把. 没有一个解决方案对我有用。我正在 Realme 2 Android 9 中测试该应用程序。请有人帮助我实现这一目标。android:forceDarkAllowed=false
MainActivity
解决方案
扩展您的应用程序类并调用AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
它的onCreate
方法。像这样:
public class App: Application() {
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
}
不要忘记在清单中注册它:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourpackage">
<application
android:name=".App" // <----- Your application class name
// more stuff here >
</application>
</manifest>
推荐阅读
- python-3.x - Impyla 以字节格式返回值
- c - C 中的位移是否仅适用于 32 位块
- r - 如何将两个数字与置信区间相乘?
- qt - 如何将 QT 与 Metal、Direct3d 和 Vulkan 等低级 3d 图形 API 一起使用?
- javascript - WebGazer.js 输入意外结束
- ios - iOS:TableView 单元格内的 TableView 不滚动
- javascript - useEffect 中的 React API 调用仅在参数被硬编码时运行,而不是在使用状态时运行
- node.js - 如何将 Angular / Node.js 应用程序复制到另一台计算机
- reactjs - 使用 styled-components 时没有重载匹配此调用
- bash - Bash 脚本在后台