在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方法中写下这段代码,你将得到屏幕的实际宽度和高度:
Display mDisplay = this.getWindowManager().getDefaultDisplay();
final int width = mDisplay.getWidth();
final int height = mDisplay.getHeight();
System.out.println("width " + width + " height " + height);