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

创建一个类,继承PopupWindow

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);
    }
}
在new出来即可使用 
centerView是你自己想要将popupWindow显示的在这个view的下面
 
  
mMenuPopupWindow = new Solve7PopupWindow(mMenuView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mMenuPopupWindow.showAsDropDown(centerView);;

你可能感兴趣的:(java,android)