我使用以下代码来获取设备的屏幕大小。这在我所有的设备上都能用,除了Galaxy S10。Galaxy S10有3040个垂直像素,这个函数返回大约2800?
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);发布于 2019-03-25 16:30:26
getSize()返回的大小不一定返回实际大小。请查看文档,了解我在下面从here引述的原因。
发布于 2020-06-11 20:07:19
您可以尝试使用以下命令:
public static int getDisplayHeight(Context context) {
WindowManager windowManager =(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
return displayMetrics.heightPixels - getNavigationBarHeight(context);
}
public static int getNavigationBarHeight(Context context) {
if (context != null && context.getResources() != null) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
return resourceId > 0 ? resources.getDimensionPixelSize(resourceId) : 0;
}
return 0;
}https://stackoverflow.com/questions/55333733
复制相似问题