键盘打开时向上移动弹出窗口

时间:2020-07-28 19:53:52

标签: java android android-activity popup

我有一个活动窗口(Activity pic),当我单击第二个按钮时,弹出窗口出现在屏幕(PopUp pic)的底部(很好,我需要),当我单击“编辑”文本字段时,调出键盘,但它涵盖了弹出窗口的编辑文本字段(键盘3)。 出现键盘时弹出窗口没有弹出的错误在哪里?你有什么主意吗?

屏幕截图

Activity

PopUp

Keyboard

弹出窗口类

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int)(width),(int)(height*.4));

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.BOTTOM;
       
        getWindow().setAttributes(params);

1 个答案:

答案 0 :(得分:0)

PopupWindow popup = new PopupWindow(
         popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
/** ... **/
popup.showAtLocation(this, Gravity.CENTER, 0, 0);

然后,您唯一需要的是键盘的侦听器(或更通用的方法:用于更改窗口高度)。这实际上比我想象的要容易-不需要像Activity对象或类似对象的任何特殊访问。即使在我只了解上下文的独立View类(我不想强制转换)中,我也能够做到这一点。您需要的一切只是一个已经添加到布局中的视图对象。

final View root = this.getRootView();
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        Rect r = new Rect();
        root.getWindowVisibleDisplayFrame(r);

        // Calculate the difference between the original height and the new height
        int heightDiff = r.height() - root.getHeight();

        // Now update the Popup's position
        // The first value is the x-axis, which stays the same.
        // Second value is the y-axis. We still want it centered, so move it up by 50% of the height
        // change
        // The thir`enter code here`d and the fourth values are default values to keep the width/height
        popup.update(0, heightDiff / 2, -1, -1);
    }
});