我创建了一个自定义键盘,工作正常 – 除了前两行键的预览视图显示不够高.它们的垂直位置受到父布局的约束.
这些屏幕截图说明了问题 – “0”和“8”的预览位置是好的,但对于“5”和“2”,它不是:
键’0’的预览显示在按钮上方…
键’8’的预览也显示在按钮上方…
但键’5’的预览未显示在按钮上方…
键’2’的预览未显示在按钮上方…
如何克服,因此’5’和’2’的预览显示在它们各自键上方相同的距离处,与’0’和’8’相同.
这是我的keyboard.xml …
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 …
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布局……
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">
解决方法:
预览键实际上是在KeyboardView的构造函数中创建的PopupWindow. (见代码here).
您看到的问题是因为PopupWindow被其父级剪辑.如果可以为PopupWindow禁用剪辑,则预览键将能够在键盘视图外“弹出”.如果在上面引用的代码中设置断点并执行以下代码,则可以看到此方法有效:mPreviewPopup.setClippingEnabled(false)
请参阅PopupWindow文档中的setClippingEnabled.默认情况下,启用剪裁.setClippingEnabled
void setClippingEnabled (boolean enabled)
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.
它显示“屏幕”,但它也适用于键盘窗口.
剩下的问题是:如何禁用剪辑?不幸的是,mPreviewPopup是一个私有变量.反思将提供访问但不理想.我还没有找到另一种方法来禁用这个私有PopupWindow的剪辑,所以这只是指向分辨率而不是分辨率本身.
更新:我再看看发生了什么.以下是我在各种API级别中发现的内容.
API 17-21:允许键盘预览弹出键盘边界.
API 22,23,25-26:键预览受限于键盘的边界,但确实完整显示.这就是OP所看到的.
API 24:键预览被约束到键盘的边界,但是被剪裁. (破碎)
因此,API 22发生了变化.我在API 21和API 22之间看到的唯一实质性差异是对FLAG_LAYOUT_ATTACHED_IN_DECOR标志的支持. (另请参阅setAttachedInDecor.)此代码确实处理弹出窗口的位置,因此可能与此问题有关. (我不再相信这是真的.)
我也发现this也可能有关系. (也许…)
setClippingEnabled()适用于API 22-26,允许预览键弹出键盘布局的边界.除了使用反射,我没有找到纠正问题的方法.
虽然新的约束行为可能是预期的行为,但这可能有资格获得错误报告.
这是一个显示问题的API 26视频,然后我在PopupWindow.java中将mClippingEnabled标志设置为false并显示已更正的行为.
来源:https://www.icode9.com/content-4-478201.html