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

简述

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

效果

  • 未修改

  • 修改后

启动样式配置

在 Flutter 项目中,启动屏幕的配置文件位于 drawable 文件夹中的 launch_background.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 样式,确保正确引用了启动屏幕配置文件:

<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 文件中,确保正确引用了修改后的样式:

<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.xmlAndroidManifest.xml 中正确引用,我们可以让应用在启动时呈现出预期的样式,提升用户体验。

相关推荐

  1. Flutter】IOS运行工程二次启动崩溃问题

    2024-04-02 09:18:04       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-02 09:18:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-02 09:18:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-02 09:18:04       82 阅读
  4. Python语言-面向对象

    2024-04-02 09:18:04       91 阅读

热门阅读

  1. windows or ubuntu mount 文件

    2024-04-02 09:18:04       38 阅读
  2. Rust常用特型之Clone+Copy特型

    2024-04-02 09:18:04       43 阅读
  3. 题目 1527: 排队打水问题

    2024-04-02 09:18:04       42 阅读
  4. 【React】路由配置之路由表与Route标签两种方式

    2024-04-02 09:18:04       45 阅读
  5. Python 运算符重载深入解析

    2024-04-02 09:18:04       41 阅读
  6. 设计一个Rust线程安全栈结构 Stack<T>

    2024-04-02 09:18:04       44 阅读
  7. 自建SSL证书(兼容ios)

    2024-04-02 09:18:04       39 阅读
  8. 【计算机网络】概述

    2024-04-02 09:18:04       39 阅读
  9. c#学习路线

    2024-04-02 09:18:04       40 阅读
  10. web3.0入门及学习路径

    2024-04-02 09:18:04       38 阅读
  11. uniapp项目--青年帮新闻项目

    2024-04-02 09:18:04       35 阅读
  12. LeetCode 2810.故障键盘:双端队列模拟

    2024-04-02 09:18:04       40 阅读