Android obtains the screen width and height, Status Bar Height, and string width and height of a mobile phone.
Android obtains the screen width and height, Status Bar Height, and string width and height of a mobile phone.
This article mainly introduces how Android can obtain the screen width and height, Status Bar Height, and string width and height of a mobile phone, this article describes how to obtain text width and height, Status Bar Height, textView width, and screen size for Android. For more information, see
First define the TextView object commentText
Get the width and height of the text:
?
1 2 3 4 5 6 7 8 |
TextPaint textPaint = new TextPaint (Paint. ANTI_ALIAS_FLAG ); TextPaint. setTextSize (commentText. getTextSize ()); TextPaint. setColor (Color. WHITE ); FontMetrics fontMetrics = textPaint. getFontMetrics (); Float fTop = fontMetrics. top; Float fBottom = fontMetrics. bottom; Float textHeight = (int) (fBottom-fTop ); Float textWidth = (int) textPaint. measureText (commentText. getText ()); |
Obtain the height of the status bar at the top of the mobile phone screen:
Copy the Code as follows:
DisplayMetrics dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );
Int width = dm. widthPixels; // screen width
Int height = dm. heightPixels; // screen height
Rect frame = new Rect ();
GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame );
Int statusBarHeight = frame. top; // high status bar
Int contentTop = getWindow (). findViewById (Window. ID_ANDROID_CONTENT). getTop ();
Int titleBarHeight = contentTop-statusBarHeight; // The height of the title bar.
Get the screen width and height of the mobile phone:
Copy the Code as follows:
WindowManager wm = (WindowManager) this. getSystemService (Context. WINDOW_SERVICE );
Int width = wm. getdefadisplay display (). getWidth (); // screen width
Int height = wm. getdefadisplay display (). getHeight (); // screen height
Get textView width
?
1 2 |
TextPaint paint = textView. getPaint (); Float len = paint. measureText (string ); |
Get screen size:
?
1 2 3 4 5 |
DisplayMetrics dm = new DisplayMetrics (); GetWindowManager (). getDefaultDisplay (). getMetrics (dm ); Double x = Math. pow (dm. widthPixels/dm. xdpi, 2 ); Double y = Math. pow (dm. heightPixels/dm. ydpi, 2 ); Double screenInches = Math. sqrt (x + y); // screen size (INCHES) |
I hope this article will help you design your Android program.