首页 > 解决方案 > 即使使用浅色主题,系统也无法在我的应用中禁用强制暗模式。如何解决?

问题描述

致版主:这不是一个重复的问题。我已经尽力了。StackOverflow 的解决方案。

我使用 Theme.MaterialComponents.Light.NoActionbar 作为 styles.xml 文件中的父主题。

我的样式.xml:

<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:navigationBarColor">#00000000</item>
        <item name="snackbarStyle">@style/MySnackbar</item>


    </style>

    <style name="AppTheme2" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:navigationBarColor">#00000000</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="snackbarStyle">@style/MySnackbar</item>

    </style>

</resources>

我的应用程序类代码:

public class Extendit extends Application {

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

应用程序类附加在清单文件中。此外,AppTheme 是应用程序的基本主题。

androidmanifest.xml:

<application
        android:name=".Extendit"
        android:allowBackup="true"
        android:appComponentFactory="df"
        android:icon="@mipmap/iconx"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/iconx"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:appComponentFactory">

我还在styles.xml 中添加了“forcedarkallowed”错误标志并删除了styles.xml(night) 文件。

PS 我正在使用 Realme 设备。

标签: androidandroid-layout

解决方案


明白了事实。问题出在设备和 Realme Android 9.0 操作系统上。它不允许您以任何方式拥有自己的白天或夜晚主题,而是迫使您拥有系统当前拥有的内容。


推荐阅读