Android 如何获得包括软键盘大小在内的屏幕分辨率?
我的手机是华为NXT-AL10,我可以从系统设置页面看到它的分辨率是1080 x 1920,但是当我试着写下面的代码来检索它的屏幕分辨率时,它的值是1080 x 1794,我想它可能不包括软键盘的大小。那么,我怎样才能像设置页面所说的那样获得分辨率(1080 x 1920)Android 如何获得包括软键盘大小在内的屏幕分辨率?,android,Android,我的手机是华为NXT-AL10,我可以从系统设置页面看到它的分辨率是1080 x 1920,但是当我试着写下面的代码来检索它的屏幕分辨率时,它的值是1080 x 1794,我想它可能不包括软键盘的大小。那么,我怎样才能像设置页面所说的那样获得分辨率(1080 x 1920) 下面的代码应该会告诉你你在寻找什么 Point size = new Point(); activity.getWindowManager().getDefaultDisplay().getRealSize(size); i
下面的代码应该会告诉你你在寻找什么
Point size = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(size);
iWidth = size.x;
iHeight = size.y;
Log.i(TAG, "Screen real size (pixels) :width = " + iWidth);
Log.i(TAG, "Screen real size (pixels) :height = " + iHeight);
请注意,这需要API 17及以上版本感谢@SimonH answer,我添加了更多逻辑来支持API 16及以下版本,这里它们仅供您参考:
private void initScreenSize() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Point size = new Point();
getWindowManager().getDefaultDisplay().getRealSize(size);
mScreenWidth = size.x;
mScreenHeight = size.y;
} else {
Display display = getWindowManager().getDefaultDisplay();
try {
Method getHeight = Display.class.getMethod("getRawHeight");
Method getWidth = Display.class.getMethod("getRawWidth");
mScreenWidth = (Integer) getHeight.invoke(display);
mScreenHeight = (Integer) getWidth.invoke(display);
} catch (Exception e) {
mScreenWidth = display.getWidth();
mScreenHeight = display.getHeight();
}
}
}
我试过了,它在API 17上运行得很好,有没有办法得到API 16上的真实尺寸?我周末出去了,刚刚看到了你的评论。我刚刚复制了一些用于为我的一个应用程序提供指标的代码。我喜欢你下面的完整回答。然而,API13中是否不推荐使用
getHeight
和getWidth
?
private void initScreenSize() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Point size = new Point();
getWindowManager().getDefaultDisplay().getRealSize(size);
mScreenWidth = size.x;
mScreenHeight = size.y;
} else {
Display display = getWindowManager().getDefaultDisplay();
try {
Method getHeight = Display.class.getMethod("getRawHeight");
Method getWidth = Display.class.getMethod("getRawWidth");
mScreenWidth = (Integer) getHeight.invoke(display);
mScreenHeight = (Integer) getWidth.invoke(display);
} catch (Exception e) {
mScreenWidth = display.getWidth();
mScreenHeight = display.getHeight();
}
}
}