Android7.0中PopupWindow弹出位置异常问题

我们在Android开发中经常会使用到PopupWindow来显示内容,在Android7.0以前。

    //    定义一个PopupWindow变量,并设置宽、高
    PopupWindow popupWindow = new PopupWindow(mWidth, mHeight);
    popupWindow.setFocusable(true);
    //    在某个控件下方弹出
    popupWindow.showAsDropDown(anchorView);

在7.0中这里的宽和高如果设置得过大,弹出的PopupWindow会覆盖当前的视窗而覆盖整个手机屏幕,并不是在anchorView的下方弹出来。
因此,为了解决这个问题,我们可以换一种方式来弹出PopupWindow。

    //    定义一个PopupWindow变量,并设置宽、高
    PopupWindow popupWindow = new PopupWindow(mWidth, mHeight);
    popupWindow.setFocusable(true);
    //    在某个控件下方弹出
  popupWindow.showAtLocation(anchorView,Gravity.LEFT,0,mNotificationBarHeight+anchorView.getHeight());

这里使用showAtLocation()来弹出PopupWindow,注意设置好x、y的偏移量(x、y默认值是0,即父窗口的左上角)。

猜你喜欢

转载自blog.csdn.net/whurs/article/details/52925328