【问题标题】:Move up Popup window when keyboard opens键盘打开时上移弹出窗口
【发布时间】:2020-07-29 05:36:32
【问题描述】:

我有一个活动窗口(活动图片),当我单击第二个按钮时,弹出窗口出现在屏幕(弹出图片)的底部(很好,我需要),当我单击编辑文本字段时,调出键盘,但它覆盖了弹出窗口编辑文本字段(键盘 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);

【问题讨论】:

    标签: java android android-activity popup


    【解决方案1】:
    PopupWindow popup = new PopupWindow(
             popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    /** ... **/
    popup.showAtLocation(this, Gravity.CENTER, 0, 0);
    

    那么您唯一需要的是键盘侦听器(或更一般地说:用于窗口高度更改)。这实际上比我想象的要容易——而且它不需要像 Activity 对象或类似的任何特殊访问。即使在我只知道上下文(我不想投射)的独立视图类中,我也能够做到这一点。您需要的只是一个已添加到布局中的 View-object。

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

    【讨论】:

    • getRootView() - 这个方法有什么作用,在哪里实现?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多