PX, DP, SP used in Android development

Source: Internet
Author: User

first introduce these units:

px : pixels (pixels), the actual pixel point on the corresponding screen.

Dip :d evice independent pixels, density independent pixels, based on the abstract unit of screen density.

On a monitor that is 160 dots per inch.

1DP = 1px, which is 1:1 relationship. (DP is DIP)

sp : scaled pixels (enlarge pixels). Pixels independent of density and font scaling. Mainly used for font display of best for textsize.

Because the Android device screen clutter, a variety of pattern width and height of a lot of not, plus a variety of cottage.

When a set of layout has been developed. The painful thing comes, the screen fits.

How do you ensure that layout is displayed properly at different resolutions of the screen? Let's briefly analyze some of the frequently used units in Android.


PX (pixels) is the unit used by the UI designer to cut graphs in PS, and the display unit is the same in the phone screen.

DP is the unit of dimension used when developing layout in Android.

The SP is the font size unit that is used when developing layout, and when the font unit is set to SP, the font can be actively stretched to fit with the screen size.


Android supports five different dpi modes: ldpi, MDPI, hdpi, xhdpi, xxhdpi

In general, the phone resolution matches the DPI mode that is being performed. This means that different DPI directories are loaded into the res according to the size of the screen.

For example, HVGA (320x480 pixels) of the phone screen is generally around 3.5 inches. Execution in MDPI mode (with exception of exotic machines)

(This is ROM-controlled.) App cannot be changed).

When executed under MDPI,1dp=1px: That is, the designer defines an item height of 48px in PS. At development time, the 14PX font in the item high 48dp;photoshop is defined and is defined as 14SP at development time.


For a WVGA (480x800) phone, it is usually performed in hdpi mode.

When executed in hdpi mode,

1dp=1.5px: In other words, the designer defines an item in PS with a height of 72px. The item height 48DP is defined at development time;

21px large font in Photoshop. Development is defined as 14SP.


So, when your app needs to fit in multiple DPI modes (such as MDPI and hdpi at the same time), if you're designing (or higher) under WVGA, you need to multiply each number by 3,
All numbers (/3*2) are converted to DP when the transduction is labeled. This same set of layout can be used in two different DPI modes instead of writing two sets of layout.

The proportion:
The relationship between MDPI and hdpi is 2:3.
The relationship between MDPI and xhdpi is 1:2.
The relationship between ldpi and MDPI is 3:4.
For example, my machine red rice, 720 * 1280 screen size, is executed under the xhdpi.
An exception. Some of the more cottage pad has the possibility of a 7-inch screen with a resolution of WVGA (480*800) performed under MDPI.

PX, DP, SP used in Android development

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.