Android应用使用浅色主题仍无法禁用系统强制暗色模式,已尝试StackOverflow方案未解决求修复
我懂你的处境
你已经把能试的常规操作都做了:用亮色Material主题、加forceDarkAllowed=false、删夜间主题文件、在Application里强制设亮色模式,但在Realme设备上还是没效果?这很正常,因为ColorOS(Realme的系统)有自己独立的暗色模式逻辑,原生Android的设置管不住它。
先把你的配置贴出来方便大家参考:
你的styles.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>
你的Application类代码
public class Extendit extends Application { @Override public void onCreate() { super.onCreate(); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); } }
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">
针对Realme ColorOS的解决方案
直接上能用的步骤,按顺序来:
给主题加ColorOS专属的禁用属性
在你的AppTheme和AppTheme2里都加上这个私有属性,这是ColorOS认的开关:<item name="oppoForceDarkAllowed" tools:ignore="NewApi">false</item>加
tools:ignore="NewApi"是因为这个属性是ColorOS私有的,Android Studio会标黄警告,忽略就行。在每个Activity里强制锁亮色模式
光在Application里设全局有时候不够,ColorOS可能会在Activity层面覆盖,所以在你的BaseActivity(或者所有Activity的onCreate里)加这段代码:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 强制当前Activity用亮色模式,覆盖系统设置 getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO); }检查设备上的应用单独设置
Realme允许给单个App开暗色模式,你去设备的设置里找找:设置 > 显示与亮度 > 暗色模式 > 应用暗色模式管理
找到你的App,确保它没被强制设为暗色。清缓存重启
ColorOS有时候会缓存主题配置,清一下App的缓存,然后重启App甚至设备,让新配置生效。
最后说下
Realme的系统对暗色模式做了定制,原生的Android设置管不住它,所以必须加ColorOS专属的属性,再配合Activity层面的强制设置,基本就能解决问题了。
内容的提问来源于stack exchange,提问作者Manohar Patil

