【问题标题】:How to disable dark theme in Android Pie?(Pre Android 10)如何在 Android Pie 中禁用深色主题?(Android 10 之前)
【发布时间】:2021-08-12 07:29:03
【问题描述】:

黑暗模式官方支持来自 android 10,但我们在 Android pie 和一些 pre android 10 MIUI 中也有黑暗模式选项。 由于该应用程序不支持暗模式,我希望将其禁用。 使用以下选项,它可以在 API 29 中正常工作,但在此之前就不行了。

<item name="android:forceDarkAllowed" tools:targetApi="p">false</item>

&

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

&

android:forceDarkAllowed="false"

【问题讨论】:

  • 在您的样式中添加此“android:forceDarkAllowed="false"”后,还会在所有 XML 布局中为根视图添加背景颜色。因为如果在设备中启用了暗模式,在某些设备中可能会显示暗主题。因此,在所有 XML 布局的根视图中添加背景颜色。
  • android:forceDarkAllowed="false" 没有帮助,并且为所有 XML 添加背景看起来不像解决方案
  • 我知道向 XML 添加背景似乎不是解决方案,但在某些情况下/某些设备会显示深色主题。删除夜间主题 XML 并添加 forceDarkAllowed false 并在 manifest > application 标签中添加该样式。

标签: android


【解决方案1】:

更改主题:

<style name="Theme.MyAppparent="Theme.MaterialComponents.DayNight.DarkActionBar">

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.DarkActionBar">

如果你不需要夜间模式而不是删除'themes.xml(夜间)

【讨论】:

  • 我已经在使用
【解决方案2】:

那么在这种情况下,您可以使用以下代码以编程方式执行您喜欢的操作:

if (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES) {
      //update UI          
} else {
      //update UI
}

【讨论】:

  • 在我正在测试的设备中,它也跳过了这个测试
  • 您使用哪种设备进行测试?
  • realme c1 with Android pie,此测试在 Android 10 设备上运行良好
【解决方案3】:

将您的主题更改为Theme.MaterialComponents.Light.NoActionBar.Bridge

仅在应用程序的 onCreate 中调用此下方方法。

确保在清单文件中提及您的 Application 类

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

【讨论】:

  • 你在 android pie bcz 中尝试过吗?我已经尝试过了,但不起作用,正如我在问题本身中提到的那样。
【解决方案4】:
  • 更改themes.xml 中的值
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.DarkModeDisableForce" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>

        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/purple_500</item>
        <item name="colorSecondaryVariant">@color/purple_700</item>
        <item name="colorOnSecondary">@color/white</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->

        <!-- Disbale Dark Mode. -->
        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    </style>
</resources>

注意:

  1. 使用Theme.MaterialComponents.Light.DarkActionBar 作为父级
  2. 更改值项目colorSecondarycolorSecondaryVariantcolorOnSecondary。 (可选)
  3. 在父样式项中添加&lt;item name="android:forceDarkAllowed" tools:targetApi="q"&gt;false&lt;/item&gt;

-对于 API 29 (android 10) ** 首先,在 res/values-v29/styles.xml 中添加 style.xml ** 在样式中使用此代码

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <item name="colorPrimary">@color/colorBackground</item>  <!-- use yours -->
        <item name="colorPrimaryDark">@color/colorBackground</item><!-- use yours -->
        <item name="colorAccent">@color/accent_support</item><!-- use yours -->
        <item name="android:windowLightStatusBar">true</item>
        <item name="android:forceDarkAllowed">false</item>
        <item name="android:windowSharedElementEnterTransition">@transition/test_transition</item><!-- use yours -->
    </style>


</resources>

【讨论】:

  • 你在 android pie bcz 中尝试过吗?我已经尝试过了,但不起作用,正如我在问题本身中提到的那样。
  • 我更新了答案,在 android 10 中测试,它的工作。
  • 安卓10根本不是问题,问题出在安卓派上,安卓10官方支持暗色主题
  • 在此路径 res/values-night-v21/styles.xml 中添加 sytles.xml
猜你喜欢
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 2013-05-04
  • 2020-07-06
相关资源
最近更新 更多