我用屏幕创建android模拟器: 屏幕尺寸:7.5英寸 分辨率(宽x高):720x960 密度:mdpi 在Android虚拟设备管理器中,它显示为xlarge屏幕大小,但在以下代码中它返回大屏幕尺寸。
int screenLayout = context.getResources().getConfiguration().screenLayout;
screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
switch (screenLayout) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
return ScreenSize.SMALL;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
return ScreenSize.MEDIUM;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
return ScreenSize.LARGE;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
return ScreenSize.XLARGE;
default:
}
答案 0 :(得分:1)
您可以使用以下方式获取屏幕高度和宽度:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
答案 1 :(得分:0)
使用以下代码获取Android手机屏幕的高度和宽度。
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int height = displayMetrics.heightPixels;
int width= displayMetrics.widthPixels;
答案 2 :(得分:0)
getscreen高度和宽度使用
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
获取布局高度使用
int layoutHeight = Layout.getMeasuredHeight();
答案 3 :(得分:0)
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
答案 4 :(得分:0)
你可以试试这个:
/**
* Get screen width
*
* @param context
* @return screen width
*/
public static int getScreenWidth(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/**
* Get screen height
*
* @param context
* @return screen height
*/
public static int getScreenHeight(Context context)
{
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}