在Android应用程序中,我想知道屏幕的高度和宽度。我使用以下代码:
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
问题出在软导航栏存在时,返回的高度=实际高度 - 导航栏高度。 我想得到总价值。
注意:我使用的是Android SDK 13(v 3.2)。
答案 0 :(得分:0)
检查一下,它可能会有所帮助:
FrameLayout root = (FrameLayout) findViewById(R.id.root);
root.post(new Runnable() {
public void run() {
Rect rect = new Rect();
Window win = getWindow(); // Get the Window
win.getDecorView().getWindowVisibleDisplayFrame(rect);
// Get the height of Status Bar
int statusBarHeight = rect.top;
// Get the height occupied by the decoration contents
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
// Calculate titleBarHeight by deducting statusBarHeight from contentViewTop
int titleBarHeight = contentViewTop - statusBarHeight;
// By now we got the height of titleBar & statusBar , Now lets get the screen size
int screenHeight = dm.heightPixels;
// Now calculate the height that our layout can be set If you know that your application doesn't have statusBar added, then don't add here also.
//Same applies to application bar also
int layoutHeight = screenHeight - (titleBarHeight + statusBarHeight);
}
});
答案 1 :(得分:0)
你可以使用
display.getRealSize(size);
代替display.getSize(size);
源:
http://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)
答案 2 :(得分:0)
您可以使用DisplayMetrics。此类保存有关屏幕的信息,例如其大小,密度,字体缩放。例如
getResources().getDisplayMetrics().widthPixels
返回
The absolute width of the display in pixels.
检索高度
getResources().getDisplayMetrics().heightPixels
答案 3 :(得分:0)
在你班级的onCreate方法中写下这段代码,你将获得屏幕的实际宽度和高度:
显示mDisplay = this.getWindowManager()。getDefaultDisplay();
final int width = mDisplay.getWidth();
final int height = mDisplay.getHeight();
System.out.println("width " + width + " height " + height);