美文网首页
Android7.0中PopupWindow的showAsDro

Android7.0中PopupWindow的showAsDro

作者: ZSGZ_AD | 来源:发表于2019-01-23 11:31 被阅读9次

    在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,即父窗口的左上角)。

    方案二:

    重写showAsDropDown(view),如下:

    @Override
    public void showAsDropDown(View anchor) {
    if(Build.VERSION.SDK_INT >= 24) {
    Rect rect = new Rect();
    anchor.getGlobalVisibleRect(rect);
    int h = anchor.getResources().getDisplayMetrics().heightPixels -rect.bottom;
    setHeight(h);
    }
    super.showAsDropDown(anchor);
    }

    相关文章

      网友评论

          本文标题:Android7.0中PopupWindow的showAsDro

          本文链接:https://www.haomeiwen.com/subject/vidjjqtx.html