Android 7.0中PopupWindow设置位置不生效解决方法(亲测可用)

我的一个页面中用到了popupwindow,在我自己android7.0手机上显示有问题,在别的手机上没问题,设置位置的时候没有生效,网上查了下大多说是Android7.0的sdk本身问题(发现在小米6,系统版本8.0发现同样问题,),解决方法如下:

重写showAsDropDown(view)就解决了。

public class SupportPopupWindow extends PopupWindow {

    public SupportPopupWindow(View contentView, int width, int height){
        super(contentView,width,height);
    }

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

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        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, xoff, yoff);
    }
}

猜你喜欢

转载自blog.csdn.net/alwaysgoalong/article/details/80455427