Android screen adaptation

Source: Internet
Author: User

There are five main ways to adapt your Android screen:

1. Picture adaptation (mobile phones with different pixel densities, loading images under different resource files)

ldpi:320*240

mdpi:480*320

hdpi:800*480

xhdpi:1280*720

xxhdpi:1920*1080

If there is no picture on the phone with the current pixel density, go back and load a higher level picture

Pixel density: The pixel dpi that is currently distributed on an inch

Pixel density calculation: pixel density = number of diagonal/inches in resolution

ldpi 1dp=0.75px 160dp=120px 240px Half 320*240
mdpi 1dp=1px 160dp=160px 320px Half 480*320
hdpi 1dp=1.5px 160dp=240px 480px Half 800*480
xhdpi 1dp=2px 160dp=320px 640px Half 1280*720
xxhdpi 1dp=3px 160dp=480px 960px Half 1920*1080

2.dimens.xml distribution

For example: Personalize the xhdpi:

1. Create the "values-1280x752" folder

2. Defined in the Dimens.xml file

3. Layout adaptation (layout adaptation, different pixel density of the mobile phone, loading different resource folder layouts)

For example: Personalize the xhdpi:

1. Create the "layout-1280x752" folder

2. Define in the layout file

4.Java code adaptation (the width of the control is set according to the percentage of the screen width is high)

WindowManager WM =Getwindowmanager (); Displaymetrics Outmetrics=NewDisplaymetrics ();        Wm.getdefaultdisplay (). Getmetrics (Outmetrics); intwidth =Outmetrics.widthpixels; intHeight =Outmetrics.heightpixels; //The purpose of adding 0.5 is to round the operationLinearlayout.layoutparams Layoutparams =NewLinearlayout.layoutparams ((int) (Width * 0.5 + 0.5), (int) (Height * 0.5 + 0.5)); TextView TV=(TextView) Findviewbyid (r.id.tv); Tv.setlayoutparams (layoutparams);

5. Weight allocation (weight)

Android screen adaptation

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.