锚定PopupWindow和SOFT_INPUT_ADJUST_RESIZE android

时间:2012-11-27 11:41:21

标签: android popupwindow

我有一个PopupWindow锚定在Button(顶部)。 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

提前致谢!

2 个答案:

答案 0 :(得分:0)

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

第1部分:出现SoftKeyboard时调整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也会保持调整大小。由于PopupWindow较小,因此不会触发GlobalLayout事件。

第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;
            }
        }
    });
    

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

答案 1 :(得分:0)

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

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