我们在开发应用的时候,第一次启动应用显示黑屏或者白屏实属正常,这是因为还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 注意:添加3秒睡眠,以确保黑屏一会儿的效果明显,在项目应用要去掉这3秒睡眠
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 在这里之前,黑屏或者白屏都是window的背景颜色,是窗口背景,还没到界面的布局呢,要执行setContentView后才显示布局
setContentView(R.layout.activity_launcher);
}
解决办法:
1.设置背景图Theme
通过设置一张背景图。 当程序启动时,首先显示这张背景图,避免出现黑屏
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:screenOrientation">portrait</item>
<item name="android:windowBackground">>@mipmap/ic_icon</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
2.设置启动activity的主题
使用Layer-list制作背景图片(在style里面写一个启动页的主题)
<style name="StartAppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/start_pic_layout</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
start_pic_layout.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/ic_icon" />
</item>
</layer-list>
修改AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.lancher"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="25" />
<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>
</application>
</manifest>
就这样了,it is Done。。。