因此,我try 添加一个图像作为闪屏,我已经添加了mipmap中的某些密度文件,作为plash.png.

因此,我想为Android 12及以上以及以下Android 12处理闪屏实现,因为在Android 12中,他们引入了新的SplashScreen API.所以我得同时处理两件事.

因此,我try 通过如下实现来处理问题:

下面是res/value/styles. xml的代码:

    <!-- For devices below Android 12 -->
    <style name="SplashTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowBackground">@mipmap/splash</item>
    </style>

    <!-- For devices Android 12 or higher -->
    <style name="SplashTheme_S" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="windowSplashScreenBackground">@mipmap/splash</item>
    </style>

AndroidManifest. xml的代码:

<activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

MainActivity.kt:

package com.myorg.mynotes

import android.os.Build
import android.os.Bundle
import android.util.Log
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("MainActivity", "SDK_INT: ${Build.VERSION.SDK_INT}")
        // Dynamically set splash screen theme based on Android version
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            Log.d("MainActivity", "Using SplashTheme_S for Android 12 or higher")
            setTheme(R.style.SplashTheme_S)
            Log.d("MainActivity", "After setTheme function")
        } else{
            Log.d("MainActivity", "Using SplashTheme for Android versions below 12")
            setTheme(R.style.SplashTheme)
            Log.d("MainActivity", "After setTheme function")
        }
        super.onCreate(savedInstanceState) 
    }
}

我希望上面的代码能够动态处理闪屏,但它并没有这样做.我做错了什么?

推荐答案

所以我所做的就是把我的图像文件(splash.png)添加到mipmap文件夹(mdpi,hdpi,xhdpi,xxhdpi,xxxhdpi).然后我修改了values/styles.xml和values—night/styles.xml中的代码.

values/styles. xml中的代码更改:

<!-- Old code -->
<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:windowBackground">@drawable/launch_background</item>
</style>

<!-- New code -->
<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:windowBackground">@mipmap/splash</item>
</style>

值中的代码更改-night/style es.xml:

<!-- Old code -->
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="android:windowBackground">@drawable/launch_background</item>
</style>

<!-- New code -->
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="android:windowBackground">@mipmap/splash</item>
</style>

我还在android\app\src\main\res中创建了名为values—v31和values—night—v31的新文件夹,并在这两个文件夹中添加了styles.xml文件.

values—v31/styles. xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="LaunchTheme" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@mipmap/splash</item>
    </style>

    <style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowBackground">?android:colorBackground</item>
    </style>
</resources>

VALUES-Night-v31/style es.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="LaunchTheme" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@mipmap/splash</item>
    </style>

    <style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">?android:colorBackground</item>
    </style>
</resources>

这解决了我在android 12及以上设备和android 11及以下设备中添加图像作为闪屏的问题.

Flutter相关问答推荐

如何在不使用NULL-check(!)的情况下缩小`FutureBuilder.Builder()`内部的`Snaphot`范围

ProviderContainer和GoRouter

在Android Studio的新用户界面中未找到热重新加载

CLI数据库连接后将SHA-1密钥添加到Firebase项目

如何将Will POP示波器转换为POP数据抖动的POP示波器

我想在Flutter中画一个箭头,但我似乎无法填满空间

来自FirebaseAuth的错误DisplayName和邮箱在Flutter应用程序中给出错误

Ffmpeg_kit_fltter:^6.0.3版权问题

导航回上一页会将我带回Ffltter应用程序中的登录页面

Flutter如何将2个以上的值传递到ListView上的另一个屏幕?

Flutter-Riverpod:如何只从提供者读取一次值

Flutter在CustomPainter的右上角和左上角添加了2条曲线

Flutter中如何实现带有变化图标的浮动操作按钮?

PreferredSizeWidget类不能用作混合,因为它既不是混合类也不是混合

graphql_flutter 错误:非抽象类GraphQLWebSocketChannel缺少实现

如何在 GridView.builder 中设置 textform 字段并在 flutter 中将 12 个 textformfield 作为单个字符串获取?

对话框打开时如何使背景模糊?

如何将 List 转换为 String []?

如何在 flutter 中过滤 DateTime(intl)?

如何做到这一点,当我们在 textified 中输入 ab 然后将 applebanana 显示为一个单词?飘飘然