1. 程式人生 > >在Android 7.0上PopupWindow.showAsDropDown不起作用的解決方法

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

使用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);
    }
}