Android 锚定弹出窗口和软输入调整大小

Android 锚定弹出窗口和软输入调整大小,android,popupwindow,Android,Popupwindow,我有一个弹出窗口锚定在一个按钮(在顶部)。弹出窗口包含一个滚动视图。 PopupWindow处于软输入调整大小模式,并使用偏移进行定位 代码: window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); window.showAtLocation(parent, Gravity.NO_GRAVITY, xPos, yPos); 屏幕: 当软键盘出现时,我有以下信息(顶部按钮隐藏):

我有一个弹出窗口锚定在一个按钮(在顶部)。弹出窗口包含一个滚动视图。 PopupWindow处于软输入调整大小模式,并使用偏移进行定位

代码:

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

当软键盘出现时,我有以下信息(顶部按钮隐藏):

我希望:

PopupWindow定位在按钮上,并调整大小

提前谢谢

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

第1部分:当软键盘出现时,调整弹出窗口的大小

  • 在内容视图上使用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;
            }
        }
    });
    
  • 在服务器上注册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;
            }
        }
    });
    
    final View fakeContentView=fakeWindow.getContentView();
    fakeContentView.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener()){
    int baseHeight=0;
    @凌驾
    公共图书馆{
    
    如果(baseHeight像我说的那样,有一个更简单的解决方案:
    showAsDropDown
    。 此方法在软键盘出现时执行此操作


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

    此问题的可能解决方案是什么
     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;
            }
        }
    });