如何在 Android 上强制应用深色主题?

How do I force apply a dark theme on Android?

根据 docs,我只需要在我的 activity 清单中设置 android:forceDarkAllowed=true 并从 parent="Theme.MaterialComponents.DayNight" 继承主题。我试过了,但没用。

这是我的清单文件:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:forceDarkAllowed="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我的 styles.xml 风格:

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ...
</style>

<style name="AppTheme.Dark" parent="Theme.MaterialComponents.DayNight">
    ...
</style>

我尝试使用以下代码获取 activity 的主题名称:

resources.getResourceName(
    packageManager.getPackageInfo(packageName, PackageManager.GET_META_DATA)
        .applicationInfo.theme
)

它显示 com.example.name:style/AppTheme 而不是 AppTheme.Dark。我怎样才能做到,当我 运行 应用程序时, MainActivity 自动设置自己使用 AppTheme.Dark (即黑暗模式)使用 android:forceDarkAllowed?

  1. 如果您的应用使用深色主题(例如 Theme.Material),强制深色将应用。同样,如果您的应用的主题继承自 DayNight 主题,由于自动主题切换,将应用 Force Dark。
  2. 深色主题在 Android 10(API 级别 29)及更高版本中可用。请确保您的设备的 API 级别为 29 + gradle 中的目标 SDK 为 29。
  3. 应用深色主题有两种方式:
    1. DayNight - 可自定义的主题 - 您可以控制 dark/light 主题的外观
    2. 强制深色 - 你控制浅色主题,如果用户选择深色模式,系统会在运行时将你的视图转换为深色主题。

这不是 dark mode 在 Android 10 上的工作方式。

首先,Android 不会在任何时候神奇地将 .Dark 附加到您的主题,无论用户是否启用暗模式。你有 android:theme="@style/AppTheme"。所以,Android会一直使用AppTheme,而你的AppTheme.Dark不会被使用。

其次,DayNight 背后的想法是您始终将其用作基本主题,Android 将根据用户请求在正常模式和深色模式之间切换。因此,如果您将 AppTheme 切换为扩展 DayNight,并去掉 AppTheme.Dark,您将匹配 the documentation 的要求。

android:forceDarkAllowed 适用于您无法使用 DayNight 主题的情况,并且您希望 Android 尝试自动将您的 UI 转换为黑暗的主题。如果你想保持 AppTheme 扩展 Light 而不是 DayNight,这可能有效(我个人还没有尝试使用 MaterialComponents 主题)。但是,即便如此,它也只会在用户启用暗模式时才会出现(例如,点击通知阴影图块)。

例如,在this sample app中,我使用DayNight作为我的主题:

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

</resources>

我对 res/values/ and res/values-night/ 中的那些颜色有不同的定义。我不使用 forceDarkAllowed。而且,当用户切换夜间模式时,我的 UI 来自:

至:

How can I make it so that when I run the application, the MainActivity automatically sets itself to use AppTheme.Dark (i.e. dark mode) using android:forceDarkAllowed?

你不知道。

如果您一直想要深色主题,请使用常规主题(不是 Light,不是 DayNight)。

无论当前的系统设置如何,都有允许动态设置白天或夜晚的设置。

Each of the options map directly to one of the AppCompat.DayNight modes:

Light - MODE_NIGHT_NO
Dark - MODE_NIGHT_YES
System default - MODE_NIGHT_FOLLOW_SYSTEM

To switch the theme, call AppCompatDelegate.setDefaultNightMode().

https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#change-themes

这适用于设置 DayNight 主题。如果您想利用 Android 10 中的 Force Dark,它适用于 .Light sub-themes。它不会应用于任何 DayNight 或固有的黑暗主题。

Apps must opt-in to Force Dark by setting android:forceDarkAllowed="true" in the activity's theme. This attribute is set on all of the system and AndroidX provided light themes, such as Theme.Material.Light.

https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#force-dark

在此之下,文档还指定了如何在资源或代码中排除某些项目。

Force Dark can be controlled on specific views with the android:forceDarkAllowed layout attribute or with setForceDarkAllowed().

除此之外,还有一些方法可以检测当前系统亮/暗设置并使用上述选项对其做出响应,但那是另一个 post。

对于深色主题来说,这不是一个很好的解决方案,对于与已接受答案的任何重叠,我深表歉意,但我不同意声称某些事情是不可能的,因为它不是首选。