Android gets mobile screen width, status bar height and string width information method
This article mainly introduces Android to get the phone screen width, status bar height and string width high information method, involving Android to get text width high, status bar height, textview width and screen size related skills, need friends can refer to the
First define the TextView object Commenttext
Get the width 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 ()); |
Get the status bar height above the phone screen:
Copy code code as follows:
Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
int width = dm.widthpixels; Screen width
int height = dm.heightpixels; High screen
Rect frame = new Rect ();
GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
int statusbarheight = Frame.top; Status bar Height
int contenttop = GetWindow (). Findviewbyid (window.id_android_content). GetTop ();
int titlebarheight = Contenttop-statusbarheight; High title bar
Get Phone screen width:
Copy code code as follows:
WindowManager wm = (WindowManager) this.getsystemservice (Context.window_service);
int width = Wm.getdefaultdisplay (). getwidth ();//Screen width
int height = Wm.getdefaultdisplay (). GetHeight ();//Screen height
Get TextView width
?
1 2 |
Textpaint paint = Textview.getpaint (); float len = Paint.measuretext (string); |
To get the 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 with your Android program.