【问题标题】:Is it possible to have a night-qualifier for splash screen on Android?是否可以在 Android 上设置启动画面的夜间预选赛?
【发布时间】:2019-06-29 19:10:57
【问题描述】:

背景

我知道 Android Q 上有一个新功能最终支持深色主题(写了here 来检测它)。

我也知道有“夜灯”功能 (here) 会使屏幕变得更黄。

支持手动选择主题是我多年来一直在做的事情(在 Activity 的 onCreate 的第一行代码上简单地使用 setTheme),但我想知道是否有自动的东西可以让我在之前设置它应用程序真正启动,在启动画面中。

问题

似乎一个非常古老的功能(从 API 8 开始!)在 Android 上已经存在了很长时间,在资源中有“夜间”限定符,我什至从未尝试过。

遗憾的是,因为“黑暗主题”和与夜间相关的东西现在更多地被称为新功能 (here for example),所以我找不到旧功能的全部内容。

不过,看看一些文章和文档,它似乎几乎完全是手动的:

我尝试过的

我尝试相应地设置各种主题:

res/drawable/splash.xml

  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:opacity="opaque" tools:ignore="UnusedAttribute">
    <item android:gravity="fill">
      <shape android:shape="rectangle">
        <solid android:color="#fff"/>
      </shape>
    </item>
    ...
  </layer-list>

res/drawable-v29/splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:opacity="opaque" tools:ignore="UnusedAttribute">
  <item android:gravity="fill">
    <shape android:shape="rectangle">
      <solid android:color="?attr/colorSurface"/>
    </shape>
  </item>
  ...
</layer-list>

res/drawable-night/splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:opacity="opaque" tools:ignore="UnusedAttribute">
  <item android:gravity="fill">
    <shape android:shape="rectangle">
      <solid android:color="#000"/>
    </shape>
  </item>
  ...
</layer-list>

清单

res/values/themes.xml

  <style name="AppTheme_splash" parent="@style/Theme.MaterialComponents.DayNight">
    <item name="android:windowBackground">@drawable/splash</item>
  </style>

res/values-v26/themes.xml

  <style name="AppTheme_splash" parent="@style/Theme.MaterialComponents.DayNight">
    <item name="android:windowSplashscreenContent">@drawable/splash</item>
  </style>

我试图删除android:windowBackgroundandroid:windowSplashscreenContent,试图查看它是否是自动的,但没有帮助。

问题

基本上我只是想了解夜间模式以及是否可以将其用于启动画面:

  1. Android 支持这么久的“夜间模式”什么时候开始?

  2. 是操作系统全局模式吗?用户是否控制它?

  3. 它是自动的吗?还是完全由当前应用手动操作?

  4. 即使我通过应用程序手动设置并且应用程序被杀死,该模式是否会稍后保留?

  5. 是否可以将应用程序的主题(即启动画面)设置为使用它,以便在启用夜间模式时,它会使用它来获得深色背景?

  6. 我的代码是否正确?

【问题讨论】:

  • 是的,该功能受支持,但您需要使用新的 appCompat api 以便您的样式正常工作。要设置特定模式,您需要在每次启动时设置它,这样如果用户更改主题,您应该保存首选项并在启动时恢复它。我建议您在 Application.OnCreate() 中设置模式并记住此模式不持久,您需要自己保留首选项。
  • 我忘了提到你也可以为个人活动设置它。它使用旧的夜间资源限定符,他们只是调整了新 AppCompat 中的实现以更易于使用。您的启动主题也将尊重此模式。因为您是在应用程序启动时设置的。
  • @Mihai 您能否展示它在启动画面上的工作示例?据我所知,在显示之前我不能使用任何代码,不是吗?此外,正如我所写,这与活动无关。对于 Activity,已经有多种解决方案。为什么你认为它与 appCompat 有关?它从 Android API 8 开始可用...请解释一下。究竟什么是夜间模式?不要将它与 Android Q 的黑暗主题混淆,就像我写的那样......
  • Android Q 使用旧的夜间资源限定符。在新的 AppCompat 中,他们将实现更改为在 api 级别之间更加一致,并且更易于开发人员使用(它的实现很尴尬,因为它是为 android auto 使用的)。所以总而言之,旧的夜间资源限定符被用于提供这种“新”夜间模式。
  • @StainlessSteelRat 更新了答案。忘了这个问题:)

标签: android android-night-mode


【解决方案1】:

实际上可以为启动画面添加夜间修饰符。

遗憾的是,它立即发生,在任何代码都可以使用之前。所以它完全基于操作系统的设置,而不是应用程序本身。

此外,IDE 上还有一个烦人的问题,即对于每个 Activity,它将首先使用我为启动创建的这个主题。

除了这 2 个缺点之外,一切正常,您可以在我的应用程序 here 上亲自查看。

这里:

res/drawable/splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:opacity="opaque" tools:ignore="UnusedAttribute">
    <item android:gravity="fill">
        <shape android:shape="rectangle">
            <solid android:color="#fff" />
        </shape>
    </item>
    <item
        android:width="48dp" android:height="48dp" android:gravity="center">
        <bitmap
            android:gravity="center" android:src="@mipmap/ic_launcher_foreground" />
    </item>
</layer-list>

res/drawable-night/splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:opacity="opaque" tools:ignore="UnusedAttribute">
    <item android:gravity="fill">
        <shape android:shape="rectangle">
            <solid android:color="#000" />
        </shape>
    </item>
    <item
        android:width="48dp" android:height="48dp" android:gravity="center">
        <bitmap
            android:gravity="center" android:src="@mipmap/ic_launcher_foreground" />
    </item>
</layer-list>

themes.xml

    <style name="AppTheme_splash" parent="@style/Theme.MaterialComponents.DayNight">
        <item name="android:windowBackground">@drawable/splash</item>
        <item name="android:navigationBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
        <item name="android:statusBarColor" tools:targetApi="lollipop">?android:colorBackground</item>
        <item name="colorSecondary">?colorAccent</item>
        <item name="android:colorSecondary" tools:targetApi="n_mr1">?colorAccent</item>
    </style>

清单

  <application android:theme="@style/AppTheme_splash" ...">

【讨论】:

  • 谢谢!今晚我会在我的身上试试这个!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多