Android 启动页设置非常简单
//styles.xml 设置主题
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/bg_splash</item>
<item name="android:windowFullscreen">true</item>
</style>
//activity使用主题,这时点击app图标,就会显示@drawable/bg_splash图片
<activity android:name=".MainActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
不过这么设置会存在启动页图片被拉伸和压缩的问题,比如
![](https://img-blog.csdnimg.cn/20190616230916856.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTAyMTgxNzA=,size_16,color_FFFFFF,t_70)
解决图片变形的问题,可以使用bg_splash.xml文件创建drawable代替图片
//使用layer-list添加多个图层,返回LayerDrawable
//注意:bitmap使用的图片大小要小于屏幕大小
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<solid android:color="#FFFFFF"/>
</shape>
</item>
<item
android:bottom="10dp">
<bitmap
android:gravity="bottom"
android:src="@drawable/ic_bg_splash"/>
</item>
</layer-list>
运行效果图:
没有虚拟导航栏
有虚拟导航栏
图片被虚拟导航栏遮住了, 适配:
//android:windowDrawsSystemBarBackgrounds属性是API 21以上的,需要放在values-v21文件夹里
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/bg_splash</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>
运行效果图:
![](https://img-blog.csdnimg.cn/20190617215818411.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTAyMTgxNzA=,size_16,color_FFFFFF,t_70)
Demo下载