Simple talk about the difference between SP and DP in Android _android

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.