APP启动时,在点击APP到APP启动页加载出来会有一段时间的白屏或黑屏,很丑陋。。。其实,黑屏或者白屏这里并不是不正常,而是还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景。
那window窗口背景在那里提供呢?在提供theme里面,如下提供的是白色背景,那就是启动时白屏一会儿的颜色设置。
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/white</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
所以,在theme设置windowBackground就可以解决启动时白屏黑屏一会儿了,下面提供三种解决方案:
一、提供.png背景图
提供背景图是解决的一个方法,但是要适配各种屏幕,提供很多张图片。除非图片非常复杂只能用背景图了就用这种方法吧,否则个人不建议。
二、提供.9.png(NinePatch)背景图片
如果图片不是很复杂,可以做成NinePatch图片,那就直接制作NinePatch图片,提供一张就可以适配任何手机,何乐而不为呢。
三、使用Layout-list制作背景图片
如果可以使用这种方式,推荐使用这种Layout-list制作背景图片。前2种都是使用图片占用内存啊,使用Layout-list比较省内存,做出app也不会说因为图片多体积变大吧。
AndroidManifest.xml,这里注意application使用的theme是AppTheme,而LauncherActivity使用的主题是StartAppTheme。这样做的效果是只要LauncherActivity使用StartAppTheme主题,其他Activity都是用AppTheme主题哦。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.launcheractivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LauncherActivity"
android:label="@string/app_name"
android:theme="@style/StartAppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
</application>
</manifest>
styles.xml,2个主题设置
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/white</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="StartAppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/layout_list_start_pic</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
layout_list_start_pic.xml 启动页面使用这个作为背景图片
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置整个屏幕背景为白色 -->
<item >
<color android:color="@color/white"/>
</item>
<!-- 中间logo -->
<item >
<bitmap
android:gravity="center"
android:src="@drawable/ic_launcher" />
</item>
<!-- 底部图表 -->
<item android:bottom="10dp">
<bitmap
android:gravity="bottom|center_horizontal"
android:src="@drawable/copyright" />
</item>
</layer-list>