PopupWindow在android7.0+的update、showAsDropDown方法

  • 问题描述:android 7.0上PopupWindow 调用update、showAsDropDown方法导致PopupWindow 设置的宽高无效,showAsDropDown无效,显示在屏幕左上角;
    通过查看源码可得
  • 解决方法
public void showAsDownView(View view){
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {//在7.0之前
            showAsDropDown(view, 0, 0);
        } else {
        // 适配 android 7.0
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            showAtLocation(view, Gravity.NO_GRAVITY, 0,y);
        }
    }

======================
最近遇到在API25上 showAsDropDown方法用上面解决方案不起作用,记录一下:

public  void showAsDownView(View anchor, int xoff, int yoff) {
        if (Build.VERSION.SDK_INT >= 24) {
            int[] location = new int[2];
            anchor.getLocationOnScreen(location);
            // 7.1 版本处理
            if (Build.VERSION.SDK_INT == 25) {
                //【note!】Gets the screen height without the virtual key
                WindowManager wm = (WindowManager) this.getContentView().getContext().getSystemService(Context.WINDOW_SERVICE);
                int screenHeight = wm.getDefaultDisplay().getHeight();
                /*
                /*
                 * PopupWindow height for match_parent,
                 * will occupy the entire screen, it needs to do special treatment in Android 7.1
                */
                this.setHeight(screenHeight - location[1] - anchor.getHeight() - yoff);
            }
            this.showAtLocation(anchor, Gravity.NO_GRAVITY, xoff, location[1] + anchor.getHeight() + yoff);
        } else {
            this.showAsDropDown(anchor, xoff, yoff);
        }
    }

你可能感兴趣的:(遇到的坑)