每次在写demo,运行后发现总是白屏几秒后才显示layout里所写的内容,想到demo只是很简单的东西,完全没有耗时操作,不应该那么久才显示内容。
最后找到了如下解决办法,记录如下:
1,在res/drawable目录下,新建xml文件,如splash_screen.xml:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/bg"/> <item> <bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/> </item> <item android:bottom="16dp"> <bitmap android:gravity="bottom" android:src="@mipmap/ic_launcher"/> </item> </layer-list>
第一个item是启动页的背景颜色,下面的item就是app logo或者其它部件。
2,把上面的drawable文件应用到主题window背景里去:<style name="SplashScreenTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowFullscreen">true</item> <item name="android:windowBackground">@drawable/splash_screen</item> </style>3,在AndroidManifest.xml里给启动页设置上面的主题:
<activity android:name=".SplashScreenActivity" android:theme="@style/SplashScreenTheme"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>4,SplashScreenActivity启动页里不加载任何布局,直接Intent跳转到MainActivity主页:
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); }
ok好了。
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
http://blog.csdn.net/yanzhenjie1003/article/details/52201896
github测试地址:https://github.com/XYScience/QuickSplash