Android 解决启动白屏

来源:互联网 发布:steam淘宝 编辑:程序博客网 时间:2024/06/06 14:23

之所以因为启动一个应用会白屏,是因为在加载启动Activity之前,会显示StartingWindow,我们可以通过设置Style的背景图片来解决这个问题。

这个图片自己可以随便弄,美观简约就好

<?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="@mipmap/ic_launcher" />    </item></layer-list>

设置背景图片,但记得单独弄一个style,因为所有Activity布局背景都是透明的,启动activity后可能会还能看到图片

<resources>    <!-- 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>    <style name="SplashTheme" 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>        <item name="android:windowBackground">@drawable/splash</item>    </style></resources>

单独给SplashActivity设置style

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="site.gemus.fancyview">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">        </activity>        <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>    </application></manifest>

直接启动activity,不用设置布局,为了加速启动

public class SplashActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        startActivity(new Intent(this, MainActivity.class));        finish();    }}

关于加速启动,我只有一个建议,那就是在我们自定义Application里尽量异步加载或者少放一些代码,因为应用启动先加载Application,再加载Activity

原创粉丝点击