Android PopupWindow在7.0中彈出位置問題
阿新 • • 發佈:2019-01-22
7.1版本這個bug被修復了
在7.0中寬和高如果設定得過大,彈出的PopupWindow會覆蓋當前的視窗而覆蓋整個手機螢幕,並不是在anchorView的下方彈出來。
方法一:
public void show(View view) { if (Build.VERSION.SDK_INT < 24) { popupWindow.showAsDropDown(view, 0, 0); } else { popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, view.getHeight() + getStatusBarHeight()); } } /** * 獲取狀態列高度 * @return */ private int getStatusBarHeight() { try { Resources resource = AnyfishApp.getApplication().getResources(); int resourceId = resource.getIdentifier("status_bar_height", "dimen", "Android"); if (resourceId != 0) { return resource.getDimensionPixelSize(resourceId); } } catch (Exception e) { } return 0; }
方法二:重寫popupwindow方法
@Override public void showAsDropDown(View anchorView, int xoff, int yoff) { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) { int[] a = new int[2]; anchorView.getLocationInWindow(a); showAtLocation(anchorView, Gravity.NO_GRAVITY, xoff, a[1] + anchorView.getHeight() + yoff); } else { super.showAsDropDown(anchorView, xoff, yoff); } } @Override public void showAsDropDown(View anchorView) { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) { int[] a = new int[2]; anchorView.getLocationInWindow(a); showAtLocation(anchorView, Gravity.NO_GRAVITY, 0, a[1] + anchorView.getHeight() + 0); } else { super.showAsDropDown(anchorView); } }