欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android PopupWindow的showAsDropDown位置失效的问题

程序员文章站 2022-05-31 11:45:34
...

忽然发现Android PopupWindow的showAsDropDown位置失效,据说是Android的sdk版本兼容问题,需要自己重写改方法,安卓的坑真多啊
在网上找了好多方法不管用,最后终于找到一个可以的:
贴一下解决方法,感谢
重写showAsDropDown(view)就解决了。

public class SupportPopupWindow extends PopupWindow {

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

[@Override](https://my.oschina.net/u/1162528)
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](https://my.oschina.net/u/1162528)
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);
}

}

作者:alwaysGoalong 来源:CSDN 原文:https://blog.csdn.net/alwaysGoalong/article/details/80455427

转载于:https://my.oschina.net/u/2606060/blog/3083356