From the start of the Android program, I was told that the common sense
One, DP (or dip device independent pixels)
An abstract unit based on screen density. On a 160-point display per inch, 1dp=1px. Different devices have different display effects, which is related to the device hardware.
Second, SP (Scaled Pixels)
Used primarily for font display, a pixel independent of the scale, similar to a DP, but can be scaled based on the user's font size preference.
To put it simply,
- Value of length width to use DP as unit into Dimens.xml file
- Number of font size to use SP as Unit, also into Dimens.xml file
Then, there is no then, as if the subtext is that, you remember to use on the line.
One day, when we accidentally put the font into a DP, it can work, and the effect of the same as the SP.
At this time, began to doubt, what is the difference between DP and SP what is different?
Let's do a simple sample verification, like the following, a layout code
<textview
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android: Textsize= "18SP"
android:text= "Hello world! in SP"/>
<textview android:layout_width=
"Wrap_ Content "
android:layout_height=" wrap_content "
android:textsize=" 18DP "
android:text=" Hello world! in DP "
/>
The effect is this way.
However, when we enter the system settings to modify the font for a large hour
Again into the previous interface, found something different.
From this perspective
- Using the SP as the font size unit changes with the font size of the system
- And the DP as the unit does not.
About the SP, the document is described as:
Scale-independent Pixels–this is like the DP unit, but it's also scaled by the user ' s font size preference. It is recommend to specifying font sizes, so they'll be adjusted for both the screen density and the User ' s preference.
The general meaning of
- The SP is affected by the screen density, but also by the font size of the user
- In general, it is recommended that you use an SP to follow user font size settings
So normally, we recommend using an SP as a font unit, and you can use a DP unless there are special situations where you don't want to follow the system font.