I believe every Android novice will encounter this problem, I hope this post can let you no longer tangled.
Px:
That is, the pixel, 1px represents a physical pixel on the screen;
PX units are not recommended, as the same 100px picture, the actual size displayed on different phones may be different as shown (images from Android Developer Guide, same as below).
The occasional use of PX is when you need to draw a 1-pixel table line or a hatched line, with other units such as DP will appear blurred.
Dp:
This is the most common but also the most difficult to understand the size of units. It is closely related to pixel density, so let's first explain what pixel density is. Given a cell phone with a physical size of 1.5 inches x2 inches and a screen resolution of 240x320, we can calculate the number of pixels per inch on the screen of this phone as 240/1.5=160dpi (landscape) or 320/2=160dpi (portrait), 160DPI is the pixel density of this phone, and the pixel density unit dpi is the abbreviation for dots per inch, which is the number of pixels. This value is the same for both landscape and portrait, because most phone screens use a square pixel point.
Different cell phones/tablets may have different pixel densities, such as the same as 4-inch mobile phones, with 480x320 resolution also has 800x480 resolution, the former pixel density is relatively low. The Android system defines four pixel densities: low (120DPI), Medium (160DPI), High (240dpi), and Superelevation (320DPI), and their corresponding DP-to-PX coefficients are 0.75, 1, 1.5, and 2, which are multiplied by the DP length as the number of pixels. For example, the interface has a length of "80dp" picture, then it is actually displayed as 80x1.5=120px on the 240dpi mobile phone, on the 320dpi phone actually displayed as 80x2=160px. If you compare these two phones together, you will find the physical size of the picture "almost", which is the effect of using DP as unit, see.
Update 20140701: Is the screen width of all Android phones a fixed value (such as 320DP) measured in DP? The answer is no, if you write a program to draw a horizontal line width equal to 320DP, run on different phones, you will find that some phones are shorter than the phone screen, some are longer than the screen, on the tablet and mobile phone compared to the difference is more obvious.
Dip
Exactly the same as DP, except the name is different. The dip was used in earlier versions of Android, and the DP name was later recommended for unification with SP.
Sp:
Abstract pixels independent of scale (Scale-independent Pixel). The SP and DP are similar but the only difference is that the Android system allows users to customize text size (small, normal, large, oversized, and so on), 1sp=1dp=0.00625 inches when the text size is "normal", and when the text size is "large" or "oversized", 1sp>1dp= 0.00625 inches. Similar to what we did after adjusting the font size in Windows-the window size is constant, only the text size changes.
There are a few smaller units of size:
Mm:
i.e. mm;
In
i.e. inches, 1 inches = 2.54 cm (approx);
Pt:
1pt=1/72 inches = 0.035 cm;
Best practice, the size of the text is used in SP units, non-text dimensions are used DP units . For example textsize= "16sp", layout_width= "60DP", occasionally need to use PX units, such as the need to draw a thin line on the screen when the divider:
<view layout_width= "match_parent" layout_height= "1px"/>
Finally, an Android UI design reference diagram is recommended: Android design Cheat Sheet
Resources:
Difference of PX, DP, dip and SP in Android?
Supporting multiple Screens
Displaymetrics
[Android Quiz] px, DP, and SP, what are the differences between these units?