我创建了一些自定义元素,并希望以编程方式将它们放置在右上角(距离上边缘n个像素,距离右边缘m个像素)。因此,我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
如何在主活动中获取screenWidth和screenHeight?
我创建了一些自定义元素,并希望以编程方式将它们放置在右上角(距离上边缘n个像素,距离右边缘m个像素)。因此,我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
如何在主活动中获取screenWidth和screenHeight?
当前回答
有一种使用DisplayMetrics(API 1)来实现这一点的方法并不过时,它可以避免try/catch混乱:
// initialize the DisplayMetrics object
DisplayMetrics deviceDisplayMetrics = new DisplayMetrics();
// populate the DisplayMetrics object with the display characteristics
getWindowManager().getDefaultDisplay().getMetrics(deviceDisplayMetrics);
// get the width and height
screenWidth = deviceDisplayMetrics.widthPixels;
screenHeight = deviceDisplayMetrics.heightPixels;
其他回答
对于正在搜索没有状态栏和操作栏的可用屏幕尺寸的用户(也感谢Swapnil的回答):
DisplayMetrics dm = getResources().getDisplayMetrics();
float screen_w = dm.widthPixels;
float screen_h = dm.heightPixels;
int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
screen_h -= getResources().getDimensionPixelSize(resId);
}
TypedValue typedValue = new TypedValue();
if(getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true)){
screen_h -= getResources().getDimensionPixelSize(typedValue.resourceId);
}
我认为这是最简单的
private fun checkDisplayResolution() {
val displayMetrics = DisplayMetrics().also {
windowManager.defaultDisplay.getMetrics(it)
}
Log.i(TAG, "display width: ${displayMetrics.widthPixels}")
Log.i(TAG, "display height: ${displayMetrics.heightPixels}")
Log.i(TAG, "display width dpi: ${displayMetrics.xdpi}")
Log.i(TAG, "display height dpi: ${displayMetrics.ydpi}")
Log.i(TAG, "display density: ${displayMetrics.density}")
Log.i(TAG, "display scaled density: ${displayMetrics.scaledDensity}")
}
科特林
fun getScreenHeight(activity: Activity): Int {
val metrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(metrics)
return metrics.heightPixels
}
fun getScreenWidth(activity: Activity): Int {
val metrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(metrics)
return metrics.widthPixels
}
这是我用于任务的代码:
// `activity` is an instance of Activity class.
Display display = activity.getWindowManager().getDefaultDisplay();
Point screen = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(screen);
} else {
screen.x = display.getWidth();
screen.y = display.getHeight();
}
看起来很干净,但要注意贬低。
以上代码已在API级别30中弃用。现在您可以使用以下代码
val width = windowManager.currentWindowMetrics.bounds.width()
val height = windowManager.currentWindowMetrics.bounds.height()
此方法报告包括所有系统栏区域的窗口大小,而Display#getSize(Point)报告不包括导航栏和显示剪切区域的区域。Display#getSize(Point)报告的值可通过以下方式获得:
val metrics = windowManager.currentWindowMetrics
// Gets all excluding insets
val windowInsets = metrics.windowInsets
var insets: Insets = windowInsets.getInsets(WindowInsets.Type.navigationBars())
val cutout = windowInsets.displayCutout
if (cutout != null) {
val cutoutSafeInsets = Insets.of(cutout.safeInsetLeft, cutout.safeInsetTop, cutout.safeInsetRight, cutout.safeInsetBottom)
insets = Insets.max(insets, cutoutSafeInsets)
}
val insetsWidth = insets.right + insets.left
val insetsHeight = insets.top + insets.bottom
// Legacy size that Display#getSize reports
val legacySize = Size(metrics.bounds.width() - insetsWidth, metrics.bounds.height() - insetsHeight)
来源:WindowManager#getCurrentWindowMetrics()