Dynamic acquisition of mobile phone screen width and dynamic settings control width height

Source: Internet
Author: User

1. Get the phone screen width:

Displaymetrics  New  displaymetrics ();        Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);         int screenwidth = dm.widthpixels;                   int screenheight = dm.heightpixels;

22. Set the dynamic setting height for the control according to the screen width:

= imageview.getlayoutparams ();         Params.height=SCREENWIDTH/10;          =SCREENHEIGHT/10;         Imageview.setlayoutparams (params);

Note the example:

We need to get the physical size of the screen of the Android phone or pad for the design of the interface or the implementation of other functions. Let's talk about how to get the physical size of the screen

The following code gets the size of the screen.
In an activity's OnCreate method, write the following code:

 displaymetrics metric = new   Displaymetrics ()        ;        Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric);      int  width = metric.widthpixels; //         screen width (pixels)    int  height = metric.heightpixels; //         screen height (pixels)       float  density = metric.density; //         screen density (0.75/1.0/1.5)   int  densitydpi = metric.densitydpi; //  screen density dpi (120/160/240)  

However, it should be noted that on a low-density small-screen mobile phone, only the above code is not able to get the correct size. For example, a 240x320 pixel low-density mobile phone, if you run the above code, the screen size obtained is 320x427. As a result, the study found that without multi-resolution support, the Android system would convert the 240x320 low-density (120) size to a medium-density (160) dimension, which would greatly affect the coding of the program. Therefore, it is necessary to add the Supports-screens node in the project's Androidmanifest.xml file, the specific content is as follows:

<supports-Screens            Android:smallscreens= "true"            android:normalscreens= "true"             Android:largescreens= "true"            android:resizeable= "true"            android:anydensity= " True "/>
In this case, the current Android program supports a variety of resolutions, so you can get the correct physical size. without these lines of code, no matter how you adjust the controls in layout, the phone with the corresponding resolution has no effect. Note: Due to the different Android version, some versions do not support Xlargescreens, you can directly remove the android:xlargescreens= "true".

Apricot's general code is also adapted to the screen in this way

    Private voidLayoutadjust () {ecmlog.i_ui (Class_tag,"Layoutadjust called ..."); Relativelayout.layoutparams params=(Relativelayout.layoutparams) logoiv.getlayoutparams (); Params.width= 375 * mglobaldata.mscreenwidth/1080; Params.height= 394 * mglobaldata.mscreenwidth/1080; Params.topmargin= 470 * mglobaldata.mscreenheight/1920;        Logoiv.setlayoutparams (params); Zhuanyedemimakaguanjiaiv=(ImageView) Findviewbyid (R.ID.ZHUANYEDEMIMAKAGUANJIAIV); Params=(Relativelayout.layoutparams) zhuanyedemimakaguanjiaiv.getlayoutparams (); Params.height= * mglobaldata.mscreenheight/1920; Params.width= 470 * mglobaldata.mscreenwidth/1080;    Zhuanyedemimakaguanjiaiv.setlayoutparams (params); }

Given a picture on the 1080*1820 resolution is 375*394, on the current mobile phone screen in this way to dynamically design the size of the picture.

Dynamic acquisition of mobile phone screen width and dynamic settings control width height

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.