是否可以在 Android 上为启动画面设置夜间限定符?
Is it possible to have a night-qualifier for splash screen on Android?
背景
我知道 Android Q 上有一项新功能最终支持深色主题(写 关于检测它)。
我也知道有一个"Night Light"功能(here)可以让屏幕变黄。
手动支持主题选择os是我多年来一直在做的事情(在Activity的onCreate的第一行代码中简单地使用setTheme),但我想要知道是否有一些自动功能允许我在应用程序真正启动之前在初始屏幕中进行设置。
问题
这似乎是一个非常古老的功能(自 API 8!)在 Android 上存在了很长时间,资源中有 "night" 限定符,我从来没有甚至尝试过。
遗憾的是,因为 "dark theme" 和夜间相关的东西现在更常被当作新功能来谈论 (here for example),我找不到旧功能的全部内容。
虽然看了一些文章和文档,但似乎完全ost完全是手动的:
https://developer.android.com/reference/android/app/UiModeManager.html - 说我可以自己设置,包括设置为自动。不过好像跟停靠有关,可能跟car-mode有关。
https://google-developer-training.github.io/android-developer-fundamentals-course-concepts/en/Unit%202/53_c_providing_resources_for_adaptive_layouts.html - 说我可以将 "night" 设置为限定符,并且有一个 "night mode".
我试过的
我尝试相应地设置各种主题:
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:windowBackground
和 android:windowSplashscreenContent
,试图查看它是否是自动的,但没有帮助。
问题
基本上我只想了解夜间模式,以及我是否可以将它用于启动画面:
Android支持这么久的"night mode"什么时候开始?
是os-全局模式吗?用户控制了吗?
是自动的吗?还是完全由当前应用手动操作?
即使我通过应用程序手动设置它并且应用程序被杀死,模式是否会保持稍后?
是否可以设置应用程序的主题(即启动画面)来使用它,以便在启用夜间模式时使用它有一个黑暗的背景?
我的代码是否正确?
实际上可以为启动画面设置夜间修改器。
遗憾的是,在可以使用任何代码之前,它立即发生了。所以它完全基于 OS 的设置,而不是应用程序本身的设置。
此外,IDE 上还有一个烦人的问题,即对于每个 Activity,它都会首先使用我为 splash 创建的这个主题。
除了这 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" ...">
背景
我知道 Android Q 上有一项新功能最终支持深色主题(写
我也知道有一个"Night Light"功能(here)可以让屏幕变黄。
手动支持主题选择os是我多年来一直在做的事情(在Activity的onCreate的第一行代码中简单地使用setTheme),但我想要知道是否有一些自动功能允许我在应用程序真正启动之前在初始屏幕中进行设置。
问题
这似乎是一个非常古老的功能(自 API 8!)在 Android 上存在了很长时间,资源中有 "night" 限定符,我从来没有甚至尝试过。
遗憾的是,因为 "dark theme" 和夜间相关的东西现在更常被当作新功能来谈论 (here for example),我找不到旧功能的全部内容。
虽然看了一些文章和文档,但似乎完全ost完全是手动的:
https://developer.android.com/reference/android/app/UiModeManager.html - 说我可以自己设置,包括设置为自动。不过好像跟停靠有关,可能跟car-mode有关。
https://google-developer-training.github.io/android-developer-fundamentals-course-concepts/en/Unit%202/53_c_providing_resources_for_adaptive_layouts.html - 说我可以将 "night" 设置为限定符,并且有一个 "night mode".
我试过的
我尝试相应地设置各种主题:
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:windowBackground
和 android:windowSplashscreenContent
,试图查看它是否是自动的,但没有帮助。
问题
基本上我只想了解夜间模式,以及我是否可以将它用于启动画面:
Android支持这么久的"night mode"什么时候开始?
是os-全局模式吗?用户控制了吗?
是自动的吗?还是完全由当前应用手动操作?
即使我通过应用程序手动设置它并且应用程序被杀死,模式是否会保持稍后?
是否可以设置应用程序的主题(即启动画面)来使用它,以便在启用夜间模式时使用它有一个黑暗的背景?
我的代码是否正确?
实际上可以为启动画面设置夜间修改器。
遗憾的是,在可以使用任何代码之前,它立即发生了。所以它完全基于 OS 的设置,而不是应用程序本身的设置。
此外,IDE 上还有一个烦人的问题,即对于每个 Activity,它都会首先使用我为 splash 创建的这个主题。
除了这 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" ...">