[Android Development] detailed explanation of length units in Android and usage suggestions

Source: Internet
Author: User

There are many layout units in Android, such as: (DP, SP, PX, In, PT, and mm). But what is the difference between them? When should I use it again? I think even experienced developers sometimes cannot understand it.


DPI (DOT per inch): used to describe the screen density. For example, if the screen density is 160, there are 1 inch display points per 160.


In (inch): indicates the inch, which is the physical size of the screen. Equal to 2.54 per inch
Centimeter. For example, the screen size of a mobile phone is often described as 3.2 (English), 3.5 (English), and 4 (English. These dimensions are the diagonal length of the screen. If the phone screen is 3.2 inch, the diagonal line length of the phone screen (visible area) is 3.2*2.54 = 8.128 cm.

MM: Indicates mm, which is the physical size of the screen.


PT: indicates a point, which is the physical size of the screen. PT is called a point, but it is not called a "point" in Chinese. You can view it in the English dictionary. The exact description is a special printing unit, which is 1/72 inch in size. Therefore, it is a natural standard unit of length, also known as "absolute length ". 1In
= 2.54 = 25.4 = 72pt = 6 PC.
Px (pixel): The actual pixel of the screen. For example, 320*480
The screen has 320 pixels horizontally and 480 pixels vertically. If PX is used as the unit, assuming the screen size remains unchanged (or 3.2), the initial screen density is 160, and now the screen density is 320. Then, the original textview width is set to 320 PX, at a density of 3.2
An inch screen is half shorter than a 160 inch screen with a density of 3.2.
Dip or DP: (Device
Independent pixels, device independent pixels). Generally, this parameter is used to support WVGA, hvga, and qvga, and does not depend on pixels. Note that the dip is related to the screen density DPI, and the screen density is related to the specific hardware. Incorrect hardware settings may cause the screen density DPI to fail to be properly displayed. On a display with a screen density of 160 DPI, 1dip = 1px, sometimes your screen resolution may be large, such as 480*800, but DPI If you do not set the value correctly, for example, 160 or, any dip is used to display an exception, which is usually too small. Dip conversion: 1dip
= 1px * scaleddensity.
SP (scaled pixels): it is mainly used for font display. It is a pixel unrelated to the scale. It is similar to DP, but can be scaled based on the user's font size preference. 1sp = 1px on a device whose DPI is 160; on a device whose DPI is not 160, 1sp = 1px * (current
DPI)/160.
To enable normal display of the current and future display types on the user interface, we recommend that you always use SP as the unit of text size and dip as the unit of other elements.
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.