android7.0PopWindow显示位置错误的解决方法

解决方案: 重写PopWindow的showAsDropDown()方法.

详细步骤

1.新建类MyPopWindow,继承PopupWindow.

2.重写showAsDropDown()方法.

public class MyPopWindow extends PopupWindow {


    public MyPopWindow(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);
    }

}

3.把使用PopupWindow的地方改为自己写的MyPopWindow.

猜你喜欢

转载自blog.csdn.net/huchengzhiqiang/article/details/78989038
今日推荐