The example in this article describes Android's approach to getting mobile screen width, status bar height, and string width-high information. Share to everyone for your reference. Specifically as follows:
First define the TextView object Commenttext
Get the width of the text:
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
Textpaint paint = Textview.getpaint ();
float len = Paint.measuretext (string);
To get the screen size:
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.