Android自定义键盘-预览视图仅限于父级布局 [英] Android custom keyboard - Preview view constrained to parent layout

查看:96
本文介绍了Android自定义键盘-预览视图仅限于父级布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义键盘,该键盘可以正常工作-除了最上面两行键的预览视图显示得不够高之外.它们的垂直位置受到父级布局的限制.

I have created a custom keyboard, which works fine - except the preview views for the top two rows of keys are not displayed high enough. Their vertical position is being constrained by the parent layout.

这些屏幕截图说明了问题-"0"和"8"的预览位置很好,但是对于"5"和"2"的预览位置却不是:

These screenshots illustrate the problem - the position of the preview for '0' and '8' is good, but for '5' and '2' it is not:

键"0"的预览显示在按钮上方...

按键8的预览也显示在按钮上方...

但是按键"5"的预览未显示在按钮上方...

按键2的预览未显示在按钮上方...

如何克服问题,因此"5"和"2"的预览显示在其各自键上方的距离与"0"和"8"的相同距离.

How to overcome, so the preview for '5' and '2' are displayed at the same distance above their respective key as it is for '0' and '8'.

这是我的 keyboard.xml ...

<android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:keyPreviewLayout="@layout/keyboard_key_preview" />

这是我的 keyboard_key_preview.xml ...

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@color/keyboard_preview_bg"
    android:textColor="@color/keyboard_preview_fg"
    android:textStyle="bold"
    android:textSize="@dimen/keyboard_preview_text_size" />

这是我的 keyboard_numeric.xml 布局...

<Keyboard
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="33.33%p"
    android:keyHeight="@dimen/keyboard_key_height"
    android:horizontalGap="@dimen/keyboard_horizontal_gap"
    android:verticalGap="@dimen/keyboard_vertical_gap">
    <Row android:rowEdgeFlags="top">
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="52" android:keyLabel="4" android:keyEdgeFlags="left"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="55" android:keyLabel="7" android:keyEdgeFlags="left"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9" android:keyEdgeFlags="right"/>
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="-5" android:keyIcon="@drawable/ic_backspace_white_24dp" android:isRepeatable="true" android:keyEdgeFlags="left" />
        <Key android:codes="48" android:keyLabel="0"/>
        <Key android:codes="-4" android:keyLabel="DONE" android:keyEdgeFlags="right"/>
    </Row>
</Keyboard>

推荐答案

预览键实际上是在KeyboardView的构造函数中创建的PopupWindow. (请参见此处的代码).

The preview key is actually a PopupWindow created in the constructor for KeyboardView. (See the code here).

您看到的问题是因为PopupWindow被其父项裁剪.如果可以为PopupWindow禁用剪裁,则预览键将能够弹出"(pop).在键盘视图之外.您可以看到,如果在上述代码中设置断点并执行以下代码,则该方法有效:

The problem that you are seeing is because the PopupWindow is being clipped by its parent. If clipping can be disabled for the PopupWindow then the preview key will be able to "pop" outside of the keyboard view. You can see that this approach works if you set a breakpoint at the above-referenced code and execute the following code:

mPreviewPopup.setClippingEnabled(false)

请参见 setClippingEnabled c0>.默认情况下,裁剪是启用的.

See setClippingEnabled in the documentation for PopupWindow. By default, clipping is enabled.

setClippingEnabled

setClippingEnabled

void setClippingEnabled(已启用布尔值)

void setClippingEnabled (boolean enabled)

允许弹出窗口扩展到屏幕范围之外.默认情况下,窗口被裁剪到屏幕边界.将此设置为false可以使窗口准确定位.

Allows the popup window to extend beyond the bounds of the screen. By default the window is clipped to the screen boundaries. Setting this to false will allow windows to be accurately positioned.

显示屏幕",但它也适用于键盘窗口.

It says "screen," but it applies to the keyboard window as well.

剩下的问题是:如何禁用剪辑?不幸的是,mPreviewPopup是私有变量.反思会带来机会,但并不理想.我还没有找到其他禁用此私有PopupWindow剪辑的方法,因此,这只是指向分辨率的一种方法,而不是分辨率本身.

The remaining question is: How to get clipping disabled? Unfortunately, mPreviewPopup is a private variable. Reflection will give access but is not ideal. I have not found another way to disable clipping for this private PopupWindow, so this is just pointing a way to a resolution and not the resolution itself.

更新:我又看了一下发生了什么.这是我在各种API级别中发现的.

Update: I took another look at what is going on. Here is what I found with the various API levels.

API 17-21:允许按键预览弹出键盘边界之外.

API 22、23、25-26:按键预览仅限于键盘的边界,但会完整显示.这就是OP所看到的.

API 24:按键预览仅限于键盘的边界,否则会被剪裁. (损坏)

APIs 17-21: Key preview is allowed to pop outside of the boundaries of the keyboard.

APIs 22,23,25-26: The key preview is constrained to the boundaries of the keyboard but does display in its entirety. This is what the OP was seeing.

API 24: Key preview is constrained to the boundaries of the keyboard but is otherwise clipped. (Broken)

因此,更改发生在API 22上.我看到的API 21和API 22之间的唯一实质区别是对 setAttachedInDecor .)此代码确实可以处理并放置弹出窗口,因此可能与此问题有关.. (我不再相信这是真的.)

So, the change occurred with API 22. The only substantive difference I see between API 21 and API 22 is support for the FLAG_LAYOUT_ATTACHED_IN_DECOR flag. (Also see setAttachedInDecor.) This code does deal with the placement of the popup window, so it may be related to this problem. (I no longer believe that this is true.)

我还发现了,这也可能是相关的. (也许...)

I also found this which also may be related. (Maybe...)

setClippingEnabled()在API 22-26上起作用,以允许预览键弹出到键盘布局的边界之外.除了使用反射之外,我看不到解决问题的方法.

setClippingEnabled() works on API 22-26 to permit the preview key to pop outside the boundaries of the keyboard layout. Other than using reflection, I don't see a way to correct the problem.

尽管新的受约束的行为可能是预期的行为,但这可能符合bug报告的要求.

This may qualify for a bug report although the new constrained behavior may be the expected behavior.

这里是API 26的视频,显示了该问题,然后在PopupWindow.java中将mClippingEnabled标志设置为false并显示更正的行为.

Here is a video of API 26 exhibiting the problem, then I set the mClippingEnabled flag to false in PopupWindow.java and show the corrected behavior.

这篇关于Android自定义键盘-预览视图仅限于父级布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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