本页内容

在 Android 上使用Qt Quick 实现启动画面

演示如何在 Android 上使用Qt Quick 视图设置启动画面。

带有Qt Development标志的Android启动画面,背景为绿色

此简单示例演示了如何将 Qml 视图用作 Qt 启动画面。未使用任何额外的过渡效果。在此示例中,Android 12 中引入的默认启动画面被隐藏。应用程序启动时,启动画面显示两秒钟,之后主视图会无过渡地显示出来。

本示例是展示启动画面的两个类似示例之一。其中一个使用 QML 视图,另一个使用 AndroidManifest 标记。

在 Android 上使用Qt Quick 启动画面

若要将 QML 视图用作启动画面,需定义一个视图并添加若干与启动画面功能相关的特定项。

Window {
    id: splash
    color: "#2CDE85"
    title: qsTr("Splash Window")
    modality: Qt.ApplicationModal
    flags: Qt.SplashScreen
    visible: true

    property int timeoutInterval: 2000
    signal timeout
  • modality: Qt.ApplicationModal 将视图设置为模态。
  • flags: Qt.SplashScreen 隐藏标题栏。
  • property int timeoutInterval: 2000 设置视图的可见时间。
加载主视图
    Loader {
        id: mainLoader
        source: "Main.qml"

定时器在超时后调用 exit 函数,以从启动画面切换到主视图。任何可能的退出动画都会在此处启动。

    function exit() {
        mainLoader.item.show();
        splash.visible = false
        splash.timeout()
    }

定时器为启动画面留出适当的时间显示。这可以与Loader.loaded()信号结合使用,或者在应用程序准备好显示时,与来自应用程序实现的另一个信号结合使用。

    Timer {
        interval: splash.timeoutInterval;
        running: splash.visible;
        repeat: false
        onTriggered: {
            splash.exit()
        }
    }

启动时,在应用程序的第一个视图显示之前会显示默认的启动画面。 有关修改方法的说明,请参阅Android 启动画面。
默认情况下,Android 会使用应用程序图标作为启动画面。在 Qt for Android 应用程序中,可以将其修改为应用程序的启动画面,也可以与 Qt 启动画面结合使用。

隐藏原生 Android 启动画面

为了保持不同平台间启动用户体验的一致性,可能需要隐藏 Android 启动画面。要实现这一点,请定义一个半透明主题,该主题能有效隐藏 Android 启动画面。

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="splashStartTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowDisablePreview">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>
</resources>

不过存在一个问题:如果使用半透明主题,屏幕方向的切换将无法正常工作。这是因为 Android 视图从底层视图获取其方向,而底层视图通常是应用抽屉(Application Drawer),其方向不会发生变化。 而且由于在设置 Activity 视图后无法更改主题,最终会导致应用程序被锁定在某种屏幕方向上。
作为一种变通方案,我们可以为 Android 启动画面创建一个采用半透明主题的临时 Activity。随后尽快启动另一个 Activity,该 Activity 包含应用程序所需的所有功能。

        <activity
            android:name=".SplashActivity"
            android:exported="true"
            android:label="QML Splash Screen Example"
            android:theme="@style/splashStartTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

此时,原生 Android 启动画面将不再显示,取而代之的是我们启动的 QtActivity,该 Activity 会显示在 QML 中定义的启动画面。要启动 QML 启动画面,我们必须重写 Android Activity 的onCreate 方法,并使用 Intent 来启动 QtActivity。

package io.qt.qmlsplashscreeninandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.content.Intent;
import android.view.ViewTreeObserver;
import org.qtproject.qt.android.bindings.QtActivity;

public class SplashActivity extends Activity {

    private static final String TAG = "SplashActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set up an OnPreDrawListener to the root view.
        final View content = findViewById(android.R.id.content);
                content.getViewTreeObserver().addOnPreDrawListener(
                        new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                Intent i = new Intent(SplashActivity.this, QtActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(i);

                content.getViewTreeObserver().removeOnPreDrawListener(this);
                finish();
                return true;
            }
        });
    }
}

最后,QML 启动画面会打开主视图,在本示例中,该视图仅包含一个带有关闭按钮的 View 控件。

示例项目 @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.