4

我有一个 PopupWindow 锚定在一个按钮(在顶部)。PopupWindow 包含一个 ScrollView。PopupWindow 处于 SOFT_INPUT_ADJUST_RESIZE 模式并使用偏移量定位

代码 :

    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    window.showAtLocation(parent, Gravity.NO_GRAVITY, xPos, yPos);

屏幕 :

基本画面 http://imageshack.us/a/img38/7771/basescreen.png

当软键盘出现时,我有这个(顶部按钮被隐藏):

我有什么屏幕 http://imageshack.us/a/img21/6396/whatihavescreen.png

我想拥有:

PopupWindow 锚定在 Button 上并调整大小。

我有什么屏幕 http://imageshack.us/a/img805/3302/whatiwantscreen.png

提前致谢 !

4

2 回答 2

0

自己的努力......这不是我开发的最佳解决方案,但无论如何......它有效......

第 1 部分:当软键盘出现时调整 PopupWindow 的大小

  • 在内容视图上使用 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;
            }
        }
    });
    

我很确定这是一个肮脏的解决方案,但我没有找到另一种方法。

于 2012-11-29T15:16:09.187 回答
0

就像我说的,有一个更简单的解决方案:showAsDropDown. 当软键盘出现时,此方法可以完成工作......

与之相比,唯一需要改变的showAtLocation是 xoff 和 yoff 计算。

于 2012-12-11T14:41:21.720 回答