DisplayMetrics metrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
mWidth = metrics.widthPixels;
mHeight = metrics.heightPixels;
setWidth(mWidth);
setHeight(mHeight + getStatusBarHeight(mContext) + getBottomKeyboardHeight(mContext));
popwindow.this.setClippingEnabled(false);
/**
* 获取状态栏高度
*
* @param context context
* @return 状态栏高度
*/
public static int getStatusBarHeight(Context context) {
// 获得状态栏高度
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}
/**
* 获取底部虚拟键盘的高度
*/
public static int getBottomKeyboardHeight(Activity context) {
int screenHeight = getAccurateScreenDpi(context)[1];
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
int heightDifference = screenHeight - dm.heightPixels;
return heightDifference;
}
/**
* 获取精确的屏幕大小
*/
public static int[] getAccurateScreenDpi(Activity context) {
int[] screenWH = new int[2];
Display display = context.getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
try {
Class> c = Class.forName("android.view.Display");
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
screenWH[0] = dm.widthPixels;
screenWH[1] = dm.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return screenWH;
}