Android分享笔记(2) APP启动时闪屏

出处:http://www.egef111.sh.cn/archives/95


App在启动时,即在欢迎界面。老是出现白屏或黑屏,闪一下然后才出现欢迎界面。

我欢迎界面原先是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/bg_welcome"
    android:orientation="vertical">

</LinearLayout>

把图片直接设置为背景,由于Activity只能到onResume时,才能展示到前台。所以这样直接设置为背景是会出现闪屏的,其实也不是闪屏,而是Activity的Style(白色或黑色);


是这样解决的:

  1. 首先 去掉图片设为背景,即空白layout;

  2. 定义一个Style 扩展自AppTheme,并设定windowBackground为需要显示的背景图片

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#8CC24F</item>
    <item name="colorPrimaryDark">#8CC24F</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

<style name="WelcomeTheme" parent="AppTheme">
    <item name="android:windowBackground">@mipmap/bg_welcome</item>
</style>

3.在Activity配置中引用

<activity
    android:name=".AtyWelcome"
    android:theme="@style/WelcomeTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

到此已经解决了App启动闪屏问题;

转载请注明出处: