最近做项目遇到的问题,需要一个启动页面,就是打开程序时显示一张图片,然后再跳转到主页面。
第一次做好了发现有的时候打开程序先白屏,再显示我放的那张图片,可能是因为加载布局需要时间。
后来在stack overflow上查了一下,找到了解决方案:将启动的图片写在theme里
原问题的链接:http://stackoverflow.com/questions/20432594/blank-page-on-startup
具体步骤:
将图片放到res/drawable文件夹下,我的图片文件名为splash_screen
在res/values/styles.xml文件中添加:
<style name="SplashScreen"> <item name="android:padding">0dp</item> <item name="android:windowFrame">@null</item> <item name="android:windowBackground">@drawable/splash_screen</item> <item name="android:windowNoTitle">true</item> </style>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mikewong.splashscreen" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.mikewong.splashscreen.SplashScreenActivity" android:label="@string/app_name" android:theme="@style/SplashScreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.mikewong.splashscreen.MainActivity" android:label="@string/app_name" > </activity> </application> </manifest>
package com.mikewong.splashscreen; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; public class SplashScreenActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //延迟2000ms跳转到主页面 new Handler().postDelayed(new Runnable(){ @Override public void run() { Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class); startActivity(intent); finish(); } }, 2000); } }