QML 项目固定定位

QML item fixed positioning

移动设备会在调用键盘时将元素向上移动,但是当设备的键盘出现时,有些元素会停留在相同的位置,如下图所示。

如何在设备键盘出现时将 Qml 项目固定在同一位置?

我需要 Rectangleid: principal 固定在同一位置。

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3

Window {
    visible: true

    property int larguraTela: 360
    property int alturaTela: 640

    width: larguraTela
    height: alturaTela

    maximumWidth: larguraTela
    maximumHeight: alturaTela

    minimumWidth: larguraTela
    minimumHeight: alturaTela

    title: "OverStatusBar"

    Rectangle {
        id: principal

        width: parent.width
        height: parent.height * 0.15

        anchors.top: parent.top

        color: "orange"
    }

    Rectangle {

        width: parent.width
        height: parent.height * 0.85

        anchors.top: principal.bottom

        clip: true

        Rectangle{
            id: retangulo1

            width: parent.width
            height: parent.height * 0.5

            anchors.top: parent.top

            color: "grey"
        }

        Rectangle {
            id: retangulo2

            width: parent.width
            height: parent.height * 0.5

            anchors.top: retangulo1.bottom

            color: "lightgrey"

            TextField {
                id: campoTexto

                width: parent.width * 0.7
                height: parent.height * 0.20

                anchors.centerIn: parent

                inputMethodHints: Qt.ImhDigitsOnly

            }
        }
    }
}

好的,在对这个主题进行了长时间的研究之后,我得出的结论是,至少到现在为止,没有任何可能的解决方案不涉及大量变通编程来使用跨平台编程来解决这个问题。我尝试了一堆跨平台语言,但没有令人满意的解决方案可以实施。我试过的语言是:

  • QML

  • Appcelerator(钛)

  • PhoneGap(科尔多瓦)

  • 本机脚本

  • React Native

我的结论是,如果我想开发按预期工作且没有错误的本机外观应用程序,我需要使用本机编程语言来完成,即使我需要用不同的语言开发两次。这就是我现在正在做的事情:XCode 和 Android Studio。

如果有人想看一段代码并开始在 QML 中执行它,只需访问 link clicking here:

I have something very hackish and begging for refinement but I think it is going into the right direction :

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
Window {
    visible: true
    property int larguraTela: 360
    property int alturaTela: 640
    width: larguraTela
    height: alturaTela
    maximumWidth: larguraTela
    maximumHeight: alturaTela
    minimumWidth: larguraTela
    minimumHeight: alturaTela
    title: "OverStatusBar"
    Rectangle {
        id: principal
        width: parent.width
        height: parent.height * 0.15
        anchors.top: parent.top
        color: "orange"
    }
    Timer{
        id:resetKeyboard
        interval: 500
        onTriggered: {
            Qt.inputMethod.hide();
            Qt.inputMethod.show();
            unlock.restart();
        }
    }
    Timer{
        id:unlock
        interval: 500

        onTriggered: {
            flickable.updateSlideContent = true;
        }
    }

    Flickable{
        id:flickable
        width: parent.width
        height : slideContent ? parent.height * 0.5 : parent.height * 0.85
        anchors.top: principal.bottom
        clip: true
        contentHeight: parent.height * 0.85
        contentY : slideContent ? parent.height*0.35 : 0

        property bool updateSlideContent : true
        property bool slideContent : false
        property bool keyboardVisible : Qt.inputMethod.visible
        onKeyboardVisibleChanged: {
            if (updateSlideContent) {
                slideContent = keyboardVisible;
                if (keyboardVisible)
                {
                    updateSlideContent = false;
                    resetKeyboard.restart();
                }
            }
        }

        Rectangle {

            anchors.fill: parent




            Rectangle{
                id: retangulo1

                width: parent.width
                height: parent.height * 0.5

                anchors.top: parent.top

                color: "grey"
            }

            Rectangle {
                id: retangulo2

                width: parent.width
                height: parent.height * 0.5

                anchors.top: retangulo1.bottom

                color: "lightgrey"

                TextField {
                    id: campoTexto

                    width: parent.width * 0.7
                    height: parent.height * 0.20

                    anchors.centerIn: parent

                    inputMethodHints: Qt.ImhDigitsOnly

                }
            }
        }

    }
}