在Android应用程序中,我想知道屏幕的高度和宽度。我正在使用以下代码:屏幕高度 - Android
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
问题是,当软导航栏存在时,返回的高度=实际高度 - 导航栏高度。 我想获得总价值。
注意:我正在使用Android SDK 13(v 3.2)。
在Android应用程序中,我想知道屏幕的高度和宽度。我正在使用以下代码:屏幕高度 - Android
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
问题是,当软导航栏存在时,返回的高度=实际高度 - 导航栏高度。 我想获得总价值。
注意:我正在使用Android SDK 13(v 3.2)。
检查这一点,它可以帮助:
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);
}
});
您可以使用 display.getRealSize(size);
代替display.getSize(size);
来源:
http://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)
可以使用DisplayMetrics。这个类拥有关于屏幕的信息,例如它的大小,密度,字体缩放。例如
getResources().getDisplayMetrics().widthPixels
回报
The absolute width of the display in pixels.
检索高度
getResources().getDisplayMetrics().heightPixels
在类的onCreate方法写下这段代码,你会得到屏幕的实际宽度和高度:
显示mDisplay = this.getWindowManager()。getDefaultDisplay();
final int width = mDisplay.getWidth();
final int height = mDisplay.getHeight();
System.out.println("width " + width + " height " + height);