android - 启用深色主题时,windowBackground 不会在样式中选择夜间颜色
问题描述
我已经在我的 android 应用程序中实现了深色 ui,一切正常,但我有一个启动器活动,它windowBackground
的风格像这样
<style name="AppTheme.Launcher" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowBackground">@drawable/l_launch_screen</item>
</style>
这l_launch_screen
是
<?xml version="1.0" encoding="utf-8"?>
<!-- The android:opacity=”opaque” line — this is critical in preventing a flash of black as your theme transitions. -->
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<!-- The background color, preferably the same as your normal theme -->
<item android:drawable="@color/colorPrimaryDark"/>
<!-- Your product logo - 144dp color version of your app icon -->
<item>
<bitmap
android:src="@drawable/app_round_icon"
android:gravity="center"/>
</item>
</layer-list>
并且这colorPrimaryDark
有两种颜色,一种是夜晚,另一种很简单现在问题是,当通过在 android q 通知面板中选择来激活深色主题时Dark theme
,android q 通知面板android:windowBackground
正在选择夜间颜色,但是当Dark theme
被 android 禁用并且在我的中选择了深色主题时应用程序通过在应用程序类中设置它
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
android:windowBackground
没有选择夜间颜色,但所有其他活动都在完美地选择夜间颜色我已经看到与WhatsApp
应用程序相同的行为所以这是一个错误还是我做错了什么我也尝试为夜间和可绘制定义不同的样式但正在发生同样的问题
解决方案
这是如何使用活动方法实现加载屏幕。
- 添加2个新资源文件:
- 对于浅色主题splash_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/white" />
<item>
<bitmap android:gravity="center"
android:src="@drawable/logo"/>
</item>
</layer-list>
- 对于深色主题splash_background.xml(同名)将其放在drawable-night文件夹中(如果没有则创建),并且您必须添加colors.xml自定义深色(例如
<color name="black_700">#191414</color>
):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/your_dark_background_color" />
<item>
<bitmap android:gravity="center"
android:src="@drawable/logo"/>
</item>
</layer-list>
- 最后在你的themes.xml中:
<style name="Theme.AppName.SplashTheme" parent="Theme.AppName">
<item name="android:windowBackground">@drawable/splash_background</item>
</style>
- 将启动画面创建为现有样式的主题并在应用程序启动时显示它的好方法:为此创建新的 Activity
class SplashScreenActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.Theme_AppName_SplashTheme)
super.onCreate(savedInstanceState)
startActivity(Intent(this,MainActivity::class.java))
overridePendingTransition(0, 0) //this one to remove animation
finish()
}
}
- 将此主题应用到您的 SplashScreenActivity 中
AndroidManifest.xml
:
<activity
android:name=".SplashScreenActivity"
android:excludeFromRecents="true"
android:exported="true"
android:launchMode="singleInstance"
android:theme="@style/Theme.AppName.SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.MULTIWINDOW_LAUNCHER" />
</intent-filter>
</activity>
- 在您的 MainActivity.kt 中,您必须添加 Intent.FLAG_ACTIVITY_NO_ANIMATION 以在切换活动时消除烦人的滑动动画:
companion object {
fun startFrom(context: Context) {
val intent = Intent(context, MainActivity::class.java)
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NO_ANIMATION
context.startActivity(intent)
}
}
推荐阅读
- python - 如何在 Python 中实现更快的文件 I/O?
- javascript - 安装 Nodejs 模块后如何在 HTA 中运行下一个 CMD 命令
- java - 如何在 Apache Storm 中以编程方式重新启动拓扑的工作者
- python - 有效地序列化/反序列化 SparseDataFrame
- php - Php Laravel 上传 .H、.obj、.zip 扩展文件扩展名文件
- python - 多个 for 循环来创建 pandas 数据框
- ram - 联想yoga 310-11|ap的内存可以升级吗
- python - 张量流中的前向函数
- java - 如何创建一个 SQ Lite db helper 类来创建一个简单的表
- algorithm - 递归算法的运行时间作为递归