Flutter仿Boss-1.启动黑白屏问题

简述

在使用Flutter开发App应用时,运行在Android手机启动时可能会遇到应用显示黑白屏的问题。这个问题做过Android开发的人员都知道,在Android12版本兼容中新增改用SplashScreen API定制系统启动画面,但是本文将介绍如何通过调整启动样式配置解决这个问题。

效果

  • 未修改

  • 修改后

启动样式配置

在 Flutter 项目中,启动屏幕的配置文件位于 drawable 文件夹中的 launch_background.xml。以下是一个示例配置:

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#40C2BB" />
        </shape>
    </item>

    <item android:top="150dp">
        <bitmap
            android:gravity="top|center_horizontal"
            android:src="@drawable/splash_top_icon" />
    </item>
    <item
        android:gravity="bottom|center_horizontal"
        android:bottom="50dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/splash_bottom" />
    </item>
</layer-list>

请注意,这里的配置文件决定了启动时的主题样式,包括背景颜色和图标等。在这个例子中,背景颜色为 #40C2BB,顶部图标为 splash_top_icon,底部图标为 splash_bottom。

修改启动屏幕样式

在 styles.xml 文件中修改 LaunchTheme 样式,确保正确引用了启动屏幕配置文件:

xml 复制代码
<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:windowBackground">@drawable/launch_background</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

这里通过 android:windowBackground 指定了启动屏幕的背景,确保其引用了我们之前配置的 launch_background.xml 文件。

修改 AndroidManifest.xml

在 AndroidManifest.xml 文件中,确保正确引用了修改后的样式:

xml 复制代码
<activity
    android:name=".MainActivity"
    android:theme="@style/LaunchTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

通过修改 android:theme 属性,确保启动时使用了我们修改后的样式。

结论

通过以上步骤,我们可以解决 Flutter 在 Android 平台启动时出现黑白屏的问题。通过正确配置启动屏幕样式,并确保在 styles.xml 和 AndroidManifest.xml 中正确引用,我们可以让应用在启动时呈现出预期的样式,提升用户体验。

相关推荐
恋猫de小郭1 小时前
Flutter HC 旋转 ANR,而且还和之前的 HCPP 问题类似
android·前端·flutter
恋猫de小郭1 天前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
程序员老刘2 天前
Flutter版本选择指南:尽早升级3.47.9 | 2026年11月
flutter·ai编程·客户端
恋猫de小郭2 天前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
恋猫de小郭3 天前
Flutter A2UI 1.0 ,原来的 genui 也正式换成了这一套 SDK 了
android·前端·flutter
LinXunFeng3 天前
A2UI 1.0 详解:让 AI 生成 Flutter 界面,还不怕它乱写
前端·flutter·agent
好的佩奇4 天前
Dart 之元数据与注解
android·flutter·dart
恋猫de小郭6 天前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
好的佩奇7 天前
蓝牙之设备角色与连接
android·flutter·蓝牙
SoaringHeart8 天前
Flutter最佳实践:用 flutter_html 渲染富文本
前端·flutter