Common layout units in Android

Source: Internet
Author: User


Android in the UI layout often use some units, mixing units directly affect the display of the UI, in order to correctly use each unit in the layout must first really familiar with it.

UI Display Effect factors: screen size, screen density, resolution, and Android phone kind of more, in order to adapt to different devices, we must pay attention to drawable and layout resources.

Commonly used units are: PX, dip (DP), SP, but also some less commonly used units, such as PT, in, MM.

px: corresponds to the actual pixel point on the screen;

Dip (DP): device independent pixels, an abstract unit based on screen density, 1dip=1px on 160 dots per inch, but the dip and PX conversions change as the screen density changes. Because there are different display effects in the device, the Dip unit is introduced in order to solve the problem that running on the non-resolution phone is not too large, and this unit of measurement is independent of the mobile device hardware.

SP: full name scaled pixels-best for text size, magnification pixels (scale pixels), regardless of scale, can be scaled according to the user's font size preference, mainly to handle the size of the font;

In : inches (length units);

mm: mm (unit of length);

pt: lb/dot, standard length unit,1/72 inches;

Conversion formulas for PX and DP:

PX = DP * (density/160)

Density is the screen density, which represents the number of display points per inch, different from the resolution.

For example

On the density=240,800px*480px screen, the width of the 320dp corresponds to a pixel of 480px;

On the density=160,800px*480px screen, the width of the 320dp corresponds to a pixel of 320px,

So

The use of DP (SP) can guarantee the display quality, but does not guarantee the display size ratio;

Conversely, the use of PX ensures that the display size is proportional, but does not guarantee the display effect.

Note: On the 160dpi screen, PX and DP are equivalent.

Dip (DP) should be used as a unit in the layout, whereas an SP is recommended for units defined as text size.

The influence of drawable resources on the layout

The cell phone density value (Density) indicates how many display points per inch, and the resolution of the phone is two concepts, but the resolution and density are interrelated, the conversion formula is:

The density value is 120, the screen actual resolution is: 240pxx400px (two points corresponds to a resolution);
The density value is 160, the screen actual resolution is: 320PXX533PX (3 points corresponding to two resolution);
The density value is 240, the screen actual resolution is: 480pxx800px (one point corresponds to a resolution).

For example, QVGA and WQVGA screen density value is 120,HVGA screen density value is 160,WVGA screen density value is 240.

Res resource directory, the corresponding resource file directory is different because of the different devices that are running. The real reason for this is that the resource directory is divided according to the difference in density:
The density value is 120, and the corresponding resource directory is drawable-ldpi;
The density value is 160, and the corresponding resource directory is drawable-mdpi;
The density value is 240, and the corresponding resource directory is drawable-hdpi.

screen density for different resource specifications in Android:

hdpi = 240, status bar and title bar height 19px or 25dip

MDPI = 160, status bar and title bar height 25px or 25dip

LDPI = 120, status bar and title bar height 38px or 25dip

Depending on the screen density of the device, the system chooses hdpi, MDPI, ldpi the corresponding resources, and if not labeled, the default is MDPI.

The placement of a picture resource directly affects the size of the image's final display:

For example drawable-hdpi no picture, if there is now a picture 10x10 A.png placed in the drawable-mdpi, the device screen density of 240dpi.

If you use wrap_content when you describe the picture in layout, the size of the last picture is changed to 15x15. This is because the system in DRAWABLE-HDPII can not find the picture, in drawable-mdpi find, find a.png, according to the density of different, the system automatically adjust the picture, so it becomes 15x15. If drawable-hdpi has a.png, it is not processed according to its picture size 10x10 display. Of course, if the layout describes the picture when the declaration width is 10px, then the system no matter what folder to find the picture generally does not affect the image display size (in Seekbar and other containers even if the specified size may change).

Note: Some paragraphs belong to excerpts.

Common layout units in Android

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.