I have an android app which displays a white screen for 2 seconds on startup. My other apps don't do this, but this one does. I have also implemented a splashscreen with the hope that it would fix this. Should I increase my splash screen sleep time? Thanks.
18 回答
把它放在自定义样式中,它可以解决所有问题。使用 hacky 半透明修复将使您的任务栏和导航栏半透明,并使启动画面或主屏幕看起来像意大利面条。
<item name="android:windowDisablePreview">true</item>
只需在 AndroidManifest.xml 文件中提及启动活动的透明主题即可。
喜欢:
<activity
android:name="first Activity Name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
并用Activity
class 代替AppCompatActivity
.
喜欢 :
public class SplashScreenActivity extends Activity{
----YOUR CODE GOES HERE----
}
就像你管..最初他们显示图标屏幕而不是白屏。并在 2 秒后显示主屏幕。
首先在 res/drawable 中创建一个 XML drawable。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
接下来,您将在主题中将其设置为启动活动的背景。导航到您的 styles.xml 文件并为您的启动活动添加一个新主题
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
</resources>
在新的 SplashTheme 中,将窗口背景属性设置为 XML 可绘制对象。在 AndroidManifest.xml 中将其配置为启动活动的主题:
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这个链接给出了你想要的。一步一步的程序。 https://www.bignerdranch.com/blog/splash-screens-the-right-way/
更新:
layer-list
可以像这样更简单(与标签不同,它也接受用于居中徽标的矢量可绘制对象)<bitmap>
:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Background color -->
<item android:drawable="@color/gray"/>
<!-- Logo at the center of the screen -->
<item
android:drawable="@mipmap/ic_launcher"
android:gravity="center"/>
</layer-list>
在 style.xml 中创建一个样式,如下所示:
<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
并将其与您在 AndroidManifest 中的活动一起使用:
<activity android:name=".ActivitySplash" android:theme="@style/Theme.Transparent">
您应该阅读 Cyril Mottier 的这篇精彩文章:Android 应用程序启动变得华丽
您需要自定义您Theme
的 style.xml 并避免在您的onCreate
as ActionBar.setIcon/setTitle/etc 中自定义。
另请参阅Google 的性能提示文档。
使用Trace View
和Hierarchy Viewer
查看显示视图的时间:Android Performance Optimization / Performance Tuning On Android
用于AsyncTask
显示一些视图。
这是我在示例应用程序上的 AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
如您所见,我有默认颜色,然后添加 并将其android:windowIsTranslucent
设置为true
.
据我所知,作为一名 Android 开发人员,这是您唯一需要设置的内容,以便在应用程序启动时隐藏白屏。
白色背景来自 Apptheme。您可以显示一些有用的东西,例如您的应用程序徽标而不是白屏。可以使用自定义主题来完成。在您的应用程序中添加主题即可
android:windowBackground=""
属性。属性值可以是图像或分层列表或任何颜色。
这两个属性都可以使用其中任何一个。
<style name="AppBaseThemeDark" parent="@style/Theme.AppCompat">
<!--your other properties -->
<!--<item name="android:windowDisablePreview">true</item>-->
<item name="android:windowBackground">@null</item>
<!--your other properties -->
</style>
user543 的回答很完美
<activity
android:name="first Activity Name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
但:
您的LAUNCHER Activity必须扩展Activity,而不是默认情况下的AppCompatActivity !
我在我的一个项目中也遇到了同样的问题。我通过在提供给初始屏幕的主题中添加一些以下参数来解决它。
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
你可以在我写的这篇博文中找到原因和解决方法。希望能帮助到你。
以下是建议如何设计启动画面的链接。为了避免白色/黑色背景,我们需要定义一个带有启动背景的主题,并将该主题设置为在清单文件中启动。
https://android.jlelse.eu/right-way-to-create-splash-screen-on-android-e7f1709ba154
res/drawable 文件夹中的 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/colorPrimary” />
<item>
<bitmap
android:gravity=”center”
android:src=”@mipmap/ic_launcher” />
</item>
</layer-list>
添加以下样式
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Splash Screen theme. -->
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_background</item>
</style>
在 Manifest 设置主题中如下图所示
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这解决了问题:
编辑您的 styles.xml 文件:
粘贴以下代码:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
并且不要忘记在AndroidManifest.xml文件中进行修改。(主题名称)
注意此文件中活动的声明顺序。
I encountered a similar problem and to overcome it, I implemented the code below in styles, i.e res->values->styles->resource tag
<item name="android:windowDisablePreview">true</item>
Here is the whole code:
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDisablePreview">true</item>
</style>
您应该禁用 Instant Run android studio 设置。
文件>设置>构建,执行,部署>即时运行取消选中那里显示的所有选项。
注意:Instant Run导致的白屏问题仅适用于 Debug 版本,此问题不会出现在发布版本上。
可以通过将清单中的主题设置为
<activity
android:name=".MySplashActivityName"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
之后,如果您收到
java.lang.IllegalStateException: 您需要在此活动中使用 Theme.AppCompat 主题(或后代)。
那么您可能需要在 MySplashActivity中扩展Activity而不是AppCompatActivity 。
希望能帮助到你!
白色背景是由于Android在应用程序加载到内存时启动而导致的,如果您在SplashTheme下添加这2行代码就可以避免这种情况。
<item name="android:windowDisablePreview">true</item>
<item name="android:windowIsTranslucent">true</item>
它的解决方案非常简单!
这个问题有三个基本原因
- 您正在 onCreateVeiw 函数中执行重型/长时间运行/复杂任务。
- 如果您使用的是线程。那么Thread Sleep时间可能会很大。
- 如果您使用任何第三方库。在应用程序启动时进行初始化可能会导致此问题。
解决方案:
解决方案1:
Remove the Heavy Task from onCreateView() function and place it some where appropriate place.
解决方案2:
Reduce the Thread Sleep time.
解决方案3:
Remove the Third party library at app initialize at implement them with some good strategy.
在我的情况下,我使用的是导致这个问题的 Sugar ORM。
分享改善。
试试下面的代码:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
此代码适用于我,适用于所有 Android 设备。