在Android 7.0上PopupWindow.showAsDropDown不起作用的解决方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangwenbo1019/article/details/85248602
使用popupwindow时在android7.0以上使用showAsDropDown方法不起作用,经查询需要做兼容性适配,方法如下:
public class Solve7PopupWindow extends PopupWindow {


    public Solve7PopupWindow(View mMenuView, int matchParent, int matchParent1) {
        super(mMenuView, matchParent,matchParent1);
    }

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

猜你喜欢

转载自blog.csdn.net/wangwenbo1019/article/details/85248602