我如何获得屏幕的宽度和高度,并使用这个值在:
@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
Log.e(TAG, "onMeasure" + widthSpecId);
setMeasuredDimension(SCREEN_WIDTH, SCREEN_HEIGHT -
game.findViewById(R.id.flag).getHeight());
}
我如何获得屏幕的宽度和高度,并使用这个值在:
@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
Log.e(TAG, "onMeasure" + widthSpecId);
setMeasuredDimension(SCREEN_WIDTH, SCREEN_HEIGHT -
game.findViewById(R.id.flag).getHeight());
}
当前回答
由于getMetrics和getRealMetrics已弃用,谷歌建议按如下方式确定屏幕宽度和高度:
WindowMetrics windowMetrics = getActivity().getWindowManager().getMaximumWindowMetrics();
Rect bounds = windowMetrics.getBounds();
int widthPixels = bounds.width();
int heightPixels = bounds.height();
然而,我想出了另一种方法,给我同样的结果:
Configuration configuration = mContext.getResources().getConfiguration();
Display.Mode mode = display.getMode();
int widthPixels = mode.getPhysicalWidth();
int heightPixels = mode.getPhysicalHeight();
其他回答
我发现weigan的答案最好的一个在这一页,这里是如何在Xamarin使用它。Android:
public int GetScreenWidth()
{
return Resources.System.DisplayMetrics.WidthPixels;
}
public int GetScreenHeight()
{
return Resources.System.DisplayMetrics.HeightPixels;
}
这里显示的方法已弃用/过时,但仍然可以工作。需要API 13
看看吧
Display disp= getWindowManager().getDefaultDisplay();
Point dimensions = new Point();
disp.getSize(size);
int width = size.x;
int height = size.y;
这里没有一个答案适用于Chrome OS的多显示器,或即将到来的可折叠设备。
在查找当前配置时,始终使用getResources(). getconfiguration()中当前活动的配置。不要使用来自后台活动或系统资源的配置。后台活动没有大小,系统的配置可能包含多个大小和方向冲突的窗口,因此无法提取可用的数据。
所以答案是
val config = context.getResources().getConfiguration()
val (screenWidthPx, screenHeightPx) = config.screenWidthDp.dp to config.screenHeightDp.dp
由于getMetrics和getRealMetrics已弃用,谷歌建议按如下方式确定屏幕宽度和高度:
WindowMetrics windowMetrics = getActivity().getWindowManager().getMaximumWindowMetrics();
Rect bounds = windowMetrics.getBounds();
int widthPixels = bounds.width();
int heightPixels = bounds.height();
然而,我想出了另一种方法,给我同样的结果:
Configuration configuration = mContext.getResources().getConfiguration();
Display.Mode mode = display.getMode();
int widthPixels = mode.getPhysicalWidth();
int heightPixels = mode.getPhysicalHeight();
使用这段代码,你可以得到运行时显示的宽度和高度:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
在视图中,你需要这样做:
((Activity) getContext()).getWindowManager()
.getDefaultDisplay()
.getMetrics(displayMetrics);
在某些情况下,设备有一个导航栏,你必须在运行时检查:
public boolean showNavigationBar(Resources resources)
{
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}
如果设备有一个导航栏,那么计算它的高度:
private int getNavigationBarHeight() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
return 0;
}
所以设备的最终高度是:
int height = displayMetrics.heightPixels + getNavigationBarHeight();