真正解决App启动时白屏/黑屏

背景
过去点击App图标时,App启动时总会出现短暂的白屏/黑屏,这是一个非常不好的体验,下来我们就一起来解决这个问题。

为什么
在点击App图标后,系统会首先加载AndroidManifest.xml里Activity所指定的Theme,不会那么快加载Activity里面的onCreate()方法,所以也不会那么快执行setContentView(),所以才会出现白屏/黑屏现象。

如何操作
知道这个就好办,我们只要设置好第一个Activity的Theme里的以下属性即可:

<item name="android:windowBackground">@drawable/main_window_bg</item>

main_window_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/white"/>
    <item android:top="@dimen/splash_icon_margin_top">
        <bitmap
            android:gravity="center|top"
            android:src="@drawable/ic_main_logo"/>
    </item>
</layer-list>

然后我们就会发现,成功了,白黑屏不见了,取而代之的是:


image.png

那么同学还想在该页面底部显示文字图片,例如一下版本信息,图片好办,直接在layer-list里添加item即可,如果想显示文字,而且还要获取要App的版本号,那么只能等待Activity加载。我们可以将Activity的Layout设置为透明,如以下布局。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="@android:color/transparent">

    <TextView
        android:id="@+id/tv_copy_right"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="@dimen/splash_version_info_margin_bottom"
        android:gravity="center"
        android:textColor="#c7c7c7"
        android:textSize="13sp"/>

</FrameLayout>

</article>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,980评论 25 709
  • 在网上搜索了许多关于app启动白屏的文章大致分为两类 第一种是在style的app的theme 里设置 true ...
    licue阅读 5,792评论 0 0
  • 突然怀念小时候过年的日子,那一天天攒起来的年味儿,隔着岁月也好闻。 小时候是最有“年味”的。 那时候我们小孩子过年...
    孙瑞华阅读 648评论 0 3
  • 设备精尖高端,中西结合超县。医院快速发展,咱也增瓦添砖。
    xyx_64ee阅读 168评论 0 0
  • 我x 中午梦见了于老师 和被耍的像耗子一样的自己 太可怕了太可怕了还好是梦 一厢情愿,有始无终 面无毫损,心已成灰...
    鹿丸都有人起阅读 140评论 0 0