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);
/**
* 獲取狀態(tài)欄高度
*
* @param context context
* @return 狀態(tài)欄高度
*/
public static int getStatusBarHeight(Context context) {
? ? // 獲得狀態(tài)欄高度
? ? 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;
}