当软键盘出现在android上时适当调整主Kivy窗口的大小 [英] Properly resize main kivy window when soft keyboard appears on android

查看:137
本文介绍了当软键盘出现在android上时适当调整主Kivy窗口的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在出现软键盘时使用Window.softinput_mode调整窗口内容的大小:

I'm trying to use Window.softinput_mode to resize the window content when the soft keyboard appears:

softinput_mode = 'resize'

在此模式下,窗口将调整大小(即,窗口高度减小了keyboard_height),但键盘仍然与内容重叠,因为看起来自适应窗口y坐标减小了keyboard_height.

With this mode, the window is resized (i.e., window height is reduced by keyboard_height), but the keyboard still overlaps the content because, as it seems, the adapted window y coordinate is reduced by keyboard_height.

出现软键盘时,如何垂直对齐窗口内容?

How can i vertically top-align the window content when the soft keyboard appears?

"pan"和"below_target"模式也无济于事,因为使用它们,窗口沿y轴(+ keyboard_height)移动,因此窗口内容的顶部不可见.

The modes 'pan' and 'below_target' didn't also help, because with them the window is moved along the y-axis (+ keyboard_height) so the top part of the window content is invisible.

最少的代码(摘自kivy git-issue),说明了在Android上运行时所描述的行为: http://pastebin.com/EzKCRMj7

A minimal code (taken and adapted from a kivy git-issue), that illustrates the described behavior when ran on Android: http://pastebin.com/EzKCRMj7

推荐答案

通过放置

    if smode == 'resize':
        y += kheight

在函数 Window.update_viewport()中,我将下一个代码添加到我的应用中以进行锻炼.请勿直接更改Kivy Libs

in function Window.update_viewport(), I added the next code to my app in order to workout. DO NOT MUST CHANGE THE KIVY LIBS DIRECTLY

def update_viewport(self=Window):
    from kivy.graphics.opengl import glViewport
    from kivy.graphics.transformation import Matrix
    from math import radians

    w, h = self.system_size
    if self._density != 1:
        w, h = self.size

    smode = self.softinput_mode
    target = self._system_keyboard.target
    targettop = max(0, target.to_window(0, target.y)[1]) if target else 0
    kheight = self.keyboard_height

    w2, h2 = w / 2., h / 2.
    r = radians(self.rotation)

    x, y = 0, 0
    _h = h
    if smode == 'pan':
        y = kheight
    elif smode == 'below_target':
        y = 0 if kheight < targettop else (kheight - targettop)
    if smode == 'scale':
        _h -= kheight
    if smode == 'resize':
        y += kheight
    # prepare the viewport
    glViewport(x, y, w, _h)

    # do projection matrix
    projection_mat = Matrix()
    projection_mat.view_clip(0.0, w, 0.0, h, -1.0, 1.0, 0)
    self.render_context['projection_mat'] = projection_mat

    # do modelview matrix
    modelview_mat = Matrix().translate(w2, h2, 0)
    modelview_mat = modelview_mat.multiply(Matrix().rotate(r, 0, 0, 1))

    w, h = self.size
    w2, h2 = w / 2., h / 2.
    modelview_mat = modelview_mat.multiply(Matrix().translate(-w2, -h2, 0))
    self.render_context['modelview_mat'] = modelview_mat
    frag_modelview_mat = Matrix()
    frag_modelview_mat.set(flat=modelview_mat.get())
    self.render_context['frag_modelview_mat'] = frag_modelview_mat

    # redraw canvas
    self.canvas.ask_update()

    # and update childs
    self.update_childsize()


if __name__ == '__main__':
    app = MainApp()
    Window.update_viewport = update_viewport
    Window.softinput_mode = 'resize'
    app.run()

已经统计了 Window 的导入,并且根据不同版本的 kheight 变量可能有所不同名称.

Have in count the import of Window and depending onn the kivy version kheight variable may have a different name.

这篇关于当软键盘出现在android上时适当调整主Kivy窗口的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆