本页内容

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

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

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

这是一个简单的示例,演示了使用 Qt 显示启动画面的其中一种方法。应用启动时,带有徽标和背景色的 Qt 启动画面将显示两秒钟。随后,Qt 启动画面逐渐淡出,转而显示应用的主视图。主视图由一个简单的标签和一个关闭按钮组成。

在此示例中,Android 的默认启动画面(自 Android 12 起)被隐藏。

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

在 Android 上使用 Qt 启动画面

Android 平台的 Qt 启动画面在 AndroidManifest.xml 中进行定义。Android 项目文件可以手动创建,也可以使用Qt Creator 模板生成器创建。若要使用Qt Creator 模板生成器创建 Android 项目文件:

  1. 打开Projects
  2. 转到Build Steps 子部分
  3. 打开Build Android APK
  4. 在“Application ”子部分中选择“Create Templates ”
            <meta-data android:name="android.app.splash_screen_drawable"
                android:resource="@drawable/qtsplashscreen"/>
            <meta-data android:name="android.app.splash_screen_sticky"
                android:value="true"/>

在上述代码片段中:

自 Android 12 起,Android 拥有默认启动画面。应用启动时,会在显示应用首个视图之前先显示默认启动画面。有关修改该画面的说明和 API,请参阅Android 启动画面。
默认情况下,Android 会使用应用图标作为启动画面。 在 Qt 应用程序中,可以直接使用该启动画面,也可以修改后作为应用程序的启动画面,或者与 Qt 启动画面结合使用。为了保持不同平台之间的启动用户体验一致,可能需要隐藏 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 视图从底层视图获取其方向,而底层视图通常是应用抽屉,其方向不会改变。 而且由于在 Activity 视图设置完成后无法更改主题,最终会导致应用程序被锁定在某种屏幕方向上。
作为一种变通方案,我们可以为 Android 启动画面创建一个采用半透明主题的临时 Activity。随后尽快启动另一个 Activity,该 Activity 包含应用程序所需的所有功能。

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

要启动 Qt 启动画面,我们还需要重写 Android Activity 的onCreate 方法,并使用 Intent 来启动 QtActivity。

package io.qt.qtsplashscreeninandroid;

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 intent = new Intent(SplashActivity.this, QtActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);

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

在此示例中,主视图仅包含一个带有退出按钮的视图。

import QtQuick
import QtQuick.Controls

Window {
    visible: true
    title: qsTr("Qt Splash Screen Example")
    color: "#2CDE85"

    Text {
        id: textLabel
        text: "First View"
        anchors.centerIn: parent
    }
    Button {
        id: closeButton
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: textLabel.bottom
        anchors.topMargin: 5
        background: Rectangle {
            color: "#00414A"
            radius: 2
        }
        text: "Close"
        contentItem: Text {
            color: "#FFFFFF"
            text: closeButton.text
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
        onClicked: {
            Qt.callLater(Qt.quit)
        }
    }
}

示例项目 @ 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.