【问题标题】:Cannot disable forced dark mode in my app by system even by using light theme. how to fix it?即使使用浅色主题,系统也无法在我的应用中禁用强制暗模式。如何解决?
【发布时间】:2021-05-30 11:30:14
【问题描述】:

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

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

我的样式.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) 文件。

附:我正在使用 Realme 设备。

【问题讨论】:

  • “我正在使用 Realme 设备”——您是否尝试过其他任何方法?也许 Realme 正在做一些不幸的事情。

标签: android android-layout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    将此添加到您的 styles.xml 文件中:

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

    并在您的 AppTheme 中使用它的父项:

    parent="Theme.MaterialComponents.Light.DarkActionBar"
    

    【讨论】:

    • "false" 这已经添加到 apptheme @3rd 行。仍然没有运气..
    • 尝试将所有代码从styles.xml(白天)复制并粘贴到styles.xml(晚上)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2020-12-26
    • 2021-01-23
    • 2020-02-12
    • 1970-01-01
    • 2020-07-03
    相关资源
    最近更新 更多