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