Android 启动页白屏优化

转自:解决 Android APP 启动页白屏问题及如何实现全屏显示_android 启动白屏-CSDN博客

一、白屏原因分析

其实,白屏现象很容易理解,在冷启动一个 APP 的时候,启动页还没完成布局文件的加载,此时显示的是 Window 窗口背景,我们看到的白屏就是 Window 窗口背景。

java 复制代码
@Override
protected void onCreate(Bundle savedInstanceState) {
 
    super.onCreate(savedInstanceState);
    // 在加载布局之前,显示的是 window 的背景
    setContentView(R.layout.activity_launcher);
 
}

Window 背景的是由 Application theme 决定的,通过设置 AndroidManifest.xml 文件里面 <application> 属性实现:

java 复制代码
android:theme="@style/AppTheme"

如下所示:背景色是白色背景,启动时就会显示白屏。

java 复制代码
<!-- 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>

其实看到这里,我们就知道怎么来解决白屏问题了,我们可以将 Window 的背景和 APP 启动页的背景设置成一样的,从视觉体验上让用户感觉一点开 APP 显示就是启动页面

二、解决白屏方案

方案一、提供 .png 背景图

如果内容布局比较复杂,可以采用背景图片的方式,但是由于存在不同尺寸和像素密度的屏幕,可能需要提供多张不同的背景图来适配各种屏幕,以避免图片拉伸变形。

如果图片不复杂,可以采用 .9.png 图片,提供一张图片就可以适配任何手机。

方案二、使用 Layout-list 制作背景

  1. 制作替代白屏的背景:bg_splash.xml
java 复制代码
<?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>
    <!-- 顶部边距200dp -->
    <item
        android:top="200dp">
        <bitmap
            android:gravity="top|center_horizontal"
            android:src="@mipmap/learn" />
    </item>
</layer-list>
  1. 将 bg_splash.xml 设为 Window 背景
java 复制代码
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- has incuded in parent style -->
        <!--
        <item name="android:windowNoTitle">true</item>
        -->
        <!-- 窗口状态栏颜色可以设置为和背景色一致 -->
        <item name="colorPrimaryDark">@color/white</item>
        <!-- 窗口的背景,替代启动时候出现的白屏 -->
        <item name="android:windowBackground">@drawable/bg_splash</item>
    </style>
  1. 将 bg_splash.xml 设为启动页 Activity 的背景
java 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_splash" >
    // 其他布局省略,你可以在背景布局上面添加其他内容
</LinearLayout>
相关推荐
游戏开发爱好者83 小时前
iOS 26 iPhone 使用记录分析 多工具组合构建全方位设备行为洞察体系
android·ios·小程序·uni-app·cocoa·iphone·webview
zhangphil10 小时前
HARDWARE 属性的Bitmap与普通Bitmap,GPU与RenderThread渲染与处理方式异同比较,Android
android
消失的旧时光-194312 小时前
Flutter 异步编程:Future 与 Stream 深度解析
android·前端·flutter
alexhilton13 小时前
Compose CameraX现已稳定:给Composer的端到端指南
android·kotlin·android jetpack
阿里云云原生14 小时前
移动端性能监控探索:可观测 Android 采集探针架构与实现
android
雨白14 小时前
玩转 Flow 操作符(一):数据转换与过滤
android·kotlin
二流小码农15 小时前
鸿蒙开发:web页面如何适配深色模式
android·ios·harmonyos
消失的旧时光-194317 小时前
TCP 流通信中的 EOFException 与 JSON 半包问题解析
android·json·tcp·数据
JiaoJunfeng17 小时前
android 8以上桌面图标适配方案(圆形)
android·图标适配
参宿四南河三17 小时前
Android Compose快速入门手册(真的只是入门)
android·app