当软键盘出现在android

时间:2016-04-21 12:44:19

标签: android python resize kivy soft-keyboard

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

softinput_mode = 'resize'

使用此模式,窗口会调整大小(即,通过keyboard_height减小窗口高度),但键盘仍然与内容重叠,因为看起来,改编后的窗口y坐标会被keyboard_height缩小。

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

模式'pan'和'below_target'也没有帮助,因为有了它们,窗口就会沿着y轴(+ keyboard_height)移动,所以窗口内容的顶部是不可见的。

编辑: 一个最小的代码(从kivy git-issue获取并改编),它说明了在Android上运行时所描述的行为: http://pastebin.com/EzKCRMj7

1 个答案:

答案 0 :(得分:0)

通过放置

解决了问题
    if smode == 'resize':
        y += kheight

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

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 变量可能有所不同名称。