New ui-get phone screen size and resolution, screen adaptation, and landscape issues

Source: Internet
Author: User

New ui-get phone screen size and common resolution, screen adaptation, the problem of the portrait and the screen

--Reprint Please indicate source: Coder-pig, welcome reprint, do not use for commercial use!


Piglet Android Development Exchange Group has been established, welcome everyone to join, both novice, rookie, Big God can, piglet a person's

Strength is limited after all, the writing out of things will certainly have a lot of flaws, welcome to point out, brainstorm, let the pig's blog

More detailed, help more people, O (∩_∩) o Thank you!

Piglet Android Development Exchange Group: Piglet Android Development Exchange Group Group number: 421858269

new Android UI Instance Daquan directory: http://blog.csdn.net/coder_pig/article/details/42145907



1) Get phone screen size and screen density:

First of all, we can be programmed to get the size of the phone screen, just the resolution Oh!!! True screen size,

How many inches this either own test, or on the Internet to check! The size of the program is just the resolution, remember!!!

Here is a tool class that provides three ways to get phone resolution and screen density, one that is obsolete and not recommended

It's used!

Let's go up and down:


Tool class:

Getscreenparameter.java

Package Com.jay.example.getscreendemo;import Android.app.activity;import Android.content.context;import Android.util.displaymetrics;import Android.util.log;import Android.view.display;public class GetScreenParameter {// Method one is obsolete, although it is available, it is not recommended to use public static void GetResolution1 (Context mcontext) {Display Mdisplay = (Activity) mcontext). Getwindowmanager (). Getdefaultdisplay (); int W = Mdisplay.getwidth (); int H = Mdisplay.getheight (); LOG.I ("GetResolution1", "Width =" + W + "px"); LOG.I ("GetResolution1", "Height =" + H + "px");} Method Two is to obtain the screen size by getwindowmanager public static void GetResolution2 (Context mcontext) {Displaymetrics mdisplaymetrics = New Displaymetrics ();((Activity) mcontext). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Mdisplaymetrics); int W = Mdisplaymetrics.widthpixels;int H = mdisplaymetrics.heightpixels;float density = mdisplaymetrics.density; Screen density (0.75/1.0/1.5) int densitydpi = mdisplaymetrics.densitydpi; is the screen density *160, screen density dpi (120/160/240) log.i ("Getresolution2 "," Width = "+ W +" px "); LOG.I ("GetResolution2", "Height =" + H + "px"); LOG.I ("GetResolution2", "density =" + density); LOG.I ("GetResolution2", "densitydpi =" + densitydpi);} Method Three is to get the screen size by getresources public static void GetResolution3 (Context mcontext) {displaymetrics mdisplaymetrics = new Displaymetrics (); mdisplaymetrics = Mcontext.getresources (). Getdisplaymetrics (); int W = Mdisplaymetrics.widthpixels; int H = mdisplaymetrics.heightpixels;float density = mdisplaymetrics.density; Screen density (0.75/1.0/1.5) int densitydpi = mdisplaymetrics.densitydpi; is the screen density *160, screen density dpi (120/160/240) log.i ("GetResolution3", "Width =" + W + "px"); LOG.I ("GetResolution3", "Height =" + H + "px"); LOG.I ("GetResolution3", "density =" + density); LOG.I ("GetResolution3", "densitydpi =" + densitydpi);}}

Mainactivity.java

Package Com.jay.example.getscreendemo;import Android.app.activity;import Android.os.bundle;public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Getscreenparameter.getresolution1 (Mainactivity.this); Getscreenparameter.getresolution2 (Mainactivity.this); Getscreenparameter.getresolution3 (Mainactivity.this);}}

usage is very simple, here just print to logcat, we can change according to their own needs, there is another point to note is:

For some small screen low-density mobile phones, such as 240*320 's mobile phone, the calculated size may be: 320*427, and the Internet has a solution,

The reason for this is that the Android system will convert the 240x320 low density (120) size to medium density without setting multi-resolution support.

(160) The corresponding size, so to obtain the correct physical size, only need to add the following content in the Androidmanifest.xml file:

<supports-screensandroid:smallscreens= "true" android:normalscreens= "true" android:largescreens= "true" Android: Resizeable= "true" android:anydensity= "true"/>


can solve this problem!




2) Phone resolution problem:

I believe that after you create the project, you can see the res directory of drawable there are several

at this point you may have some questions about what these folders are for . Why are there so many points?

A: These folders are put drawable resources, you can understand to put pictures, divided so many are to

Different image resources are loaded for phones with different screen densities, because the same picture

Different screen densities may have different results under the mobile phone, which is obviously contrary to our original intent; Android

The image resources under the corresponding folder will be loaded according to the different DPI of the phone!




After the xdpi is written to load the corresponding folder of the picture Resources!

Of course, this is not absolute, for example, we leave all the pictures in the drawable-hdpi, even if the phone

Should load the ldpi folder under the picture resources, but ldpi not, then the load will be hdpi under the picture!

In addition, there is a situation: For example, hdpi,mdpi directory, ldpi under, then will load MDPI resources!

The principle is to use the closest density level!

PS: In addition if you want to prohibit Android from loading different folder resources without following screen density, just androidmanifest.xml

Add the Android:anydensity= "false" field to the file!




3) Screen fit problem:

Believe that screen adaptation is a headache for everyone, it is said that the nether surface development should pay attention to the place it!

① draw the interface when using the Relativelayout + linearlayout Weight (weight) attribute to write!


② Multi-use match_parent and warp_content, if the size of the dead control must be written, use DP instead of px (pixels)

For text size use SP, if we do not set TEXTSIZE, the default size is 14sp!

(If the text size is larger than 15SP, you can also use DP, you like!)


③ for different screen density of mobile phones to provide different sizes of pictures, from low to high ratio: 3,4,6,8;

For example: ldpi under the picture for 36*36, then MDPI is 48*48, the same hdpi as 72*72,xhdpi for 96*96


④ use different layouts for different phones, just create a layout folder named:

layout-480x320 such a format, and then write the corresponding layout file, layout-320x240 so that the Android system will be based on

Different phone resolutions load different layouts! Also add this item to the Androidmanifest.xml:

<supports-screens android:smallscreens= "true" android:normalscreens= "true" android:largescreens= "true" Android: Xlargescreens= "true" android:anydensity= "true"/>
otherwise it would have no effect!


⑤ for locally stretched pictures, such as the most common chat textview, the text too long can cause tensile deformation, then

You should use the 9-patch tool to process images at these times!

PS: Regarding the use of 9patch tools, you can reach: 9patch (9 sister) Usage




4. Load different layouts on the screen:

Layout-port: Horizontal screen layout-land: Vertical screen

When the phone loads, it will judge the screen, then load different layouts.






New ui-get phone screen size and resolution, screen adaptation, and landscape issues

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.