The screen size of Android mobile phones has always been a headache for developers. Because the screen sizes used by mobile phone manufacturers are different, the presentation and layout of user UI interfaces are naturally different. Therefore, when developing Android mobile appsProgramIn addition to understanding the underlying API, the most important thing is the definition of screen resolution.
Android can be set to adjust the zoom ratio with the window size, but even so, the mobile phone program designer must still clearly know the border of the mobile phone screen to avoid Layout) deformation Problem. In Android, only a few lines are required.CodeThe cell phone screen resolution can be obtained. The key is the displaymetrics application.
The displaymetrics class is directly inherited from the object class and stored in the Android. util package. Displaymetrics objects record some common information, including the display information, size, dimension, and font, as shown in the following table:
Static int |
Default_density The reference density used throughout the system. |
Float |
Density The logical density of the display. |
Int |
Heightpixels The absolute height of the display in pixels. |
Float |
Scaleddensity A scaling factor for fonts displayed on the display. |
Int |
Widthpixels The absolute width of the display in pixels. |
Float |
Xdpi The exact physical pixels per inch of the screen in the X dimension. |
Float |
Ydpi The exact physical pixels per inch of the screen in the Y dimension. |
It is worth mentioning that widthpixels and heightpixels record the screen width and height of the mobile phone. We can use these two values to get the screen resolution of the mobile phone.Note that the pixels here refer to absolute (absolute) pixels rather than relative pixels.
The following code gets the screen resolution:
Public class mainactivity extends activity {private textview text = NULL; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); super. setcontentview (R. layout. activity_main); this. TEXT = (textview) super. findviewbyid (R. id. text); displaymetrics dm = new displaymetrics (); super. getwindowmanager (). getdefadisplay display (). getmetrics (DM); string stropt = "cell phone screen resolution:" + DM. widthpixels + "*" + DM. heightpixels; this. text. settext (stropt );}}
The layout file is very simple. Just use a textview component:
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: paddingbottom = "@ dimen/activity_vertical_margin" Android: paddingleft = "@ dimen/plugin" Android: paddingright = "@ dimen/plugin" Android: paddingtop = "@ dimen/plugin" tools: context = ". mainactivity "> <textview Android: Id =" @ + ID/text "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "/> </relativelayout>
Program running effect: