简体   繁体   English

IME的部分自定义Android视图已被切断?

[英]Part of custom Android view for IME being cut off?

I'm working on an IME for Android and for that purpose I've created a custom view. 我正在为Android输入IME,为此,我创建了一个自定义视图。

I'm setting up the view in an XML file like this: 我在这样的XML文件中设置视图:

<com.paldepind.mykeyboard.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
/>

I want the view to be 40% the height of the screen. 我希望视图是屏幕高度的40%。 Thus in my custom view I'm overriding the onMeasure method that views have: 因此,在我的自定义视图中,我将覆盖视图具有的onMeasure方法:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = (int) (View.MeasureSpec.getSize(heightMeasureSpec) * .4);
    setMeasuredDimension(widthMeasureSpec,
                         View.MeasureSpec.makeMeasureSpec(height,
                         View.MeasureSpec.EXACTLY));
}

When viewed on the phone the view correctly begins 60% down on the screen – but it is then being cut off a little further down as shown in the screenshot below. 在电话上查看时,屏幕上的视图正确地开始向下60%的位置,但随后将其截断了一点,如下面的屏幕截图所示。

Android屏幕截图

If I change the .4 in my code above to just 1 the view does however fill up the entire screen as expected. 如果我将上面代码中的.4更改为1则视图确实会按预期填满整个屏幕。 It seems that the lower the number is, the more of the view is being cut off. 似乎数字越小,被截断的视图越多。

I really can't figure out why this wouldn't work. 我真的不知道为什么这行不通。 Any help is much appreciated! 任何帮助深表感谢!

It seems that the onMeasure method was for some reason begin called twice. 似乎onMeasure方法由于某种原因而被调用了两次。 The first time the heightMeasureSpec parameter was equal to the screen size and the second time the parameter was set to the before set height of the view. 第一次将heightMeasureSpec参数等于屏幕尺寸,第二次将该参数设置为视图的heightMeasureSpec设置的高度。 Thus the view ended up being only 40% off 40% of the screen size. 因此,视图最终仅是屏幕尺寸40%的40%。

I have fixed the issue by getting the screen size specifically in the onMeasure method call: 我通过在onMeasure方法调用中获取屏幕尺寸来解决此问题:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int height = (int) (size.y * .4);
    setMeasuredDimension(widthMeasureSpec,
                         View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
}

Try to put the keyboardview inside a relative layout. 尝试将键盘视图放入相对布局中。 Maybe can solve it. 也许可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM