【发布时间】:2019-04-23 08:30:44
【问题描述】:
当 Cordova android 应用程序启动时,在 cordova-plugin-splashscreen 启动之前,一个空白屏幕短暂可见。我了解到这是 windowBackground 颜色,并且可以通过创建自定义 styles.xml 并通过活动的 android:theme 属性在 AndroidManifest.xml 中引用它来进行更改。示例:
来自 AndroidManifest.xml:
<activity android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize" android:label="@string/activity_name" android:launchMode="singleTask" android:name="MainActivity" android:screenOrientation="portrait" android:theme="@style/CustomStyle" android:windowSoftInputMode="adjustPan">
来自styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomStyle" parent="@android:style/Theme.Material.Light.NoActionBar">
<item name="android:windowBackground">@drawable/init_splash</item>
</style>
</resources>
styles.xml 引用了另一个仅包含可绘制颜色的文件。
这行得通。它允许我更改初始屏幕之前出现的颜色。
但是,我现在希望允许用户选择性地更改为深色主题。我已经想出了如何修改 cordova-plugin-splashscreen 以根据用户偏好更改启动画面,但是我遇到了麻烦在运行时以编程方式更改 windowBackground/theme。
我尝试在 MainActivity.java 或 CordovaActivity.java 中添加以下内容:
setTheme(R.style.CustomDarkStyle);
getWindow().setBackgroundDrawableResource(getResources().getIdentifier("init_splash_dark", "drawable", getPackageName()));
getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
我将这些放在了 onCreate 之前的 super.onCreate() 或 setContentView() 中。窗口背景颜色确实发生了变化,但在启动前的初始空白屏幕保持在清单中设置的任何颜色。
如何在应用程序启动时以编程方式更改活动/窗口背景颜色?
Some have suggested 将应用主题更改为透明主题以完全防止黑屏,但这会导致应用打开延迟。我对空白屏幕很好,我只想以编程方式更改它的颜色。
截至 4 月 22 日,我还没有找到解决此问题的方法。
【问题讨论】: