我有一个 PopupWindow 锚定在一个按钮上(在顶部)。 PopupWindow 包含一个 ScrollView。 PopupWindow 处于 SOFT_INPUT_ADJUST_RESIZE 模式并使用偏移定位
代码:
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
window.showAtLocation(parent, Gravity.NO_GRAVITY, xPos, yPos);
屏幕:
base screen http://imageshack.us/a/img38/7771/basescreen.png
当软键盘出现时,我有这个(顶部按钮被隐藏):
what i have screen http://imageshack.us/a/img21/6396/whatihavescreen.png
我想要:
PopupWindow 锚定在 Button 上并调整了大小。
what i have screen http://imageshack.us/a/img805/3302/whatiwantscreen.png
提前致谢!
最佳答案
自己努力... 这不是我开发的最佳解决方案,但无论如何......它有效......
第 1 部分:出现 SoftKeyboard 时调整 PopupWindow 的大小
在内容 View 上使用 OnGlobalLayoutListener
contentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { int baseHeight = 0; @Override public void onGlobalLayout() { if(resized) { return; } if(baseHeight <= 0) { baseHeight = contentView.getHeight(); return; } final int diff = baseHeight - contentView.getHeight(); if(diff > 0) { // keyboard is visible window.update( - 1, baseHeight - diff - yPos); resized = true; } } });
完成此操作后,即使隐藏了 SoftKeyboard,PopupWindow 也会保持调整大小。 GlobalLayout 事件未触发,因为 PopupWindow 较小。
第 2 部分: 使用伪造的 PopupWindow 来了解 SoftKeyboard 是否被隐藏(脏 :()
- 使用真实的 PopupWindow 高度构建假的 PopupWindow
- 当真品被驳回时,别忘了驳回假货
先假后真
buildFakePopupWindow(rootHeight); window.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { if(fakeWindow != null) { fakeWindow.dismiss(); } } }); fakeWindow.showAtLocation(parent, Gravity.NO_GRAVITY, xPos, yPos);
在假的上注册一个 GlobalLayoutListener
final View fakeContentView = fakeWindow.getContentView(); fakeContentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { int baseHeight = 0; @Override public void onGlobalLayout() { if(baseHeight <= 0) { baseHeight = fakeContentView.getHeight(); return; } final int diff = baseHeight - fakeContentView.getHeight(); if(diff <= 0 && resized) { window.update( - 1, WindowManager.LayoutParams.WRAP_CONTENT); resized = false; } } });
我很确定这是一个肮脏的解决方案,但我没有找到另一种方法。
关于android - 锚定 PopupWindow 和 SOFT_INPUT_ADJUST_RESIZE android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13583473/