安卓应用启动时黑屏和白屏的原因及解决办法

我们在开发应用的时候,第一次启动应用显示黑屏或者白屏实属正常,这是因为还没加载到布局文件,就已经显示了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。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值