Summary of usage of Android DP and SP

Source: Internet
Author: User

1 "

DP is a unit that is independent of pixel density, that is, in the same size, but different resolution of the phone, the thing with the DP logo, the size of the display is the same.

The SP is used to identify the font, not only with respect to the screen size, but also with the system font size set.

[HTML]View Plaincopy
  1. <TextView
  2. android:id="@+id/tv"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="Wahahahhahahahahhah"
  6. android:textsize="15SP"
  7. />
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="Wahahahhahahahahhah"
  12. android:textsize="15DP"
  13. android:layout_below="@+id/tv"
  14. />

System font size is medium (default OH)

When you change to extra large:

So the font size to use SP Oh ~

2 "

Android to set the font size, the use of the SP or DP? Most people are sure to blurt out, using SP Ah, fools know to use SP instead of DP!!!

So why? Some might say that Google has specifically defined the SP as the unit to describe the font size, Google recommends this!

What I want to say is that you may be misled by Google. Let's prove it through experiments:

First I demonstrate the SP, I think the main text size in the ListView item is set to 18SP, and then we look at his reactions on different screens:

1. Nexus 5

2. Huawei a low-end machine

There is no discovery, Huawei machine fonts significantly larger, a bit out of line with expectations Ah, there are wood!!!

Below we change the SP to DP, that is, the font size changed to 18DP, and then look at the effect:

1. Nexus 5

2. Huawei a low-end machine

It is obvious that with DP, the experience is more consistent on high-end and low-end machines, and the font size ratio is more appropriate!!!

Of course, I do not want to contend with Google, have to use the DP without SP, in fact, in the actual development, we should still use more than the SP less DP, only in the font is relatively large, such as more than 15SP, we can consider using DP to try.

3 "

    1. DP is also a dip, which is basically similar to SP.

      You can use a DP or SP if you set properties that represent length, height, and so on. However, if you set the font, you need to use the SP.

    2. 2

      The DP is not density-independent, and the SP is not related to scale, except for density. If the screen density is 160, then the DP and SP and PX are the same. 1DP=1SP=1PX, but if you use PX as the unit, if the screen size is constant (assuming 3.2 inches), the screen density becomes 320. So the original width of the TextView is set to 160px, in the density of 320 of the 3.2-inch screen is more than the density of 160 of the 3.2-inch screen to see the half-short. But if it is set to 160DP or 160SP. The Width property value is automatically set to 320px. That means 160 * 320/160. 320/160 of these are called density scale factors.

    3. 3

      If you use DP and SP, the system will automatically convert according to the change of screen density

Summary of usage of Android DP and SP

Related Article

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.