One, px
Pixels, we often say 400*800 this is the pixel, this is better understood.
Second, DP
To understand DP, first introduce the concept of DPI, the DPI is the dots per inch, the number of pixels per inch diagonal, so it is calculated as follows:
For example, height and width are long and wide pixels, the sum of squares is the number of pixels diagonal, size that we often say 5 inch mobile phone, 4 inch mobile phone 5 and 4, that is, the length of the diagonal.
Obviously, the same 5-inch phone, the higher the resolution, the higher the DPI.
DP is also called DIP, which is device independent pixels. The device does not depend on one unit of pixels.
Defines the 1PX=1DP on dpi=160 devices, so the conversion formula for PX and DP is: PX = DP * (dpi/160)
In this way, 2-1, after using the pixel units, the different devices show a different effect, in Figure 2-2, the DP as the unit, after the same DP, in different devices, after the turn into PX is equal, so there will not be the case of Figure 2-1. (Image source: http://study.163.com/course/courseLearn.htm?courseId=207001#/learn/video?lessonId=368070&courseId= 207001)
Figure 2-1:
Figure 2-2:
Third, SP
The full name is scaled pixels. This is primarily used for font sizes in projects. Using the SP as the unit, when the user changes the phone font through the phone settings, the SP will also change the font.
Summary: The size of the controls in an Android project is mainly using DP as the unit, and the font size is mostly sp.
"Android Learning" the difference between several distance units in Android layout: px, DP, SP