新建一个类extends PopupWindow,在setContentView之后,动态设置pop的宽高:
/**
* 设置PopupWindow的大小
*
* @param context
*/
private void calWidthAndHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
int mWidth = metrics.widthPixels;
//设置高度为全屏高度的70%
int mHeight = (int) (metrics.heightPixels);
// int mHeight = (int) (metrics.heightPixels * 0.7);
setWidth(mWidth);
setHeight(mHeight);
setClippingEnabled(false);
}
运行。。。。。。
发现有点美中不足,状态栏没办法铺盖,于是。。。
有2种方法:
第一种:
popupWindow.setClippingEnabled(false);
第二种:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
Field mLayoutInScreen = PopupWindow.class.getDeclaredField("mLayoutInScreen");
mLayoutInScreen.setAccessible(true);
mLayoutInScreen.set(popupWindow, true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
完美收工!
网友评论