ArticleDirectory
- Px:
- DP:
- Dip:
- SP:
- MM:
- In:
- PT:
I believe that every new Android user will encounter this problem. I hope this post will make you no longer struggle.
Px:
That is, pixels. 1px indicates a physical pixel on the screen;
The unit of PX is not recommended because the actual size of a PX image may be different on different mobile phones, as shown in.
DP:
This is the most common but hard to understand unit of size. It is closely related to "pixel density", so we will first explain what is pixel density. Suppose there is a mobile phone with a physical screen size of 1.5 inch x 2 inch and a screen resolution of 240x320, then we can calculate it on the screen of this mobile phone, the number of pixels per inch is 240/1. 5 = 160 DPI (horizontal) or 320/2 = 160 DPI (portrait). 160dpi is the pixel density of the mobile phone. The unit of pixel density DPI is the abbreviation of dots per inch, the number of pixels per inch. The horizontal and vertical values are the same, because most mobile phone screens use square pixels.
Different mobile phones/tablets may have different pixel density. For example, for a 4-inch mobile phone, x320 resolution and X resolution, the former has a relatively low pixel density. The Android system defines four pixel density types: low (120 DPI), medium (160 DPI), high (240 DPI), and ultra (320 DPI ), the coefficients from Dp to PX are 0.75, 1, 1.5, and 2 respectively. The coefficient multiplied by the DP length is the number of pixels. For example, if an image with a length of 1.5 DP is displayed on a DPI mobile phone, it is displayed as 80 x = 120px, the actual display on the 320dpi mobile phone is 80x2 = 160px. If you compare the two mobile phones, you will find that the physical size of the image is "almost". This is the effect of using DP as the unit. See.
Dip:
It is exactly the same as DP, but the name is different. Dip was used in earlier Android versions. We recommend that you use the DP name for consistency with sp.
SP:
Scale-independent pixel ). SP is similar to DP, but the only difference is that the Android system allows users to customize the text size (small, normal, large, large, and so on ), when the text size is "normal", 1sp = 1dp = 0.00625 inch, and when the text size is "large" or "super large", 1sp> 1dp = 0.00625 inch. It is similar to the effect after we adjust the font size in Windows-the window size remains unchanged, only the text size changes.
There are also a few less used units:
MM:
That is, millimeters;
In:
That is, inch, 1 inch = 2.54 cm (about );
PT:
1pt = 1/ 72 inch = 0.035 cm;
Best Practices,All text sizes are SP units, and all non-text sizes are DP units.. For example, textsize = "16sp", layout_width = "60dp", and occasionally use PX units. For example, when you need to draw a thin separator line on the screen:
<ViewLayout_width= "Match_parent"Layout_height= "1px"/>
References:
Difference of PX, DP, dip and SP in Android?
Supporting multiple screens
Displaymetrics