Android's margin and padding attributes and supported length units

Source: Internet
Author: User

The margin and padding of Android are the same as those of HTML. As shown in: the yellow part is padding, and the gray part is margin.

In general, padding is an internal border, and margin is an external border.

The corresponding property is

Android: layout_marginbottom = "25dip"
Android: layout_marginleft = "10dip"
Android: layout_margintop = "10dip"
Android: layout_marginright = "10dip"
Android: paddingleft = "1dip"
Android: paddingtop = "1dip"
Android: paddingright = "1dip"
Android: paddingbottom = "1dip"

If both the left and right are the same settings, you can set them directly.

Android: layout_margin = "10dip"
Android: padding = "5dip"

 

Length units supported by Android.

  • Px (pixel): the point on the screen.
    Pixels (pixels). Different devices have the same display effect. Generally, we use hvga to represent 320x480 pixels, which is usually used.
  • In (INCHES): the unit of length.
  • Mm (mm): the unit of length.
  • Pt (lbs): 1/72 inch.
    Point is a standard length unit, 1pt = 1/72 inch, used in the printing industry, very easy to use;
  • DP (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.
  • Dip: it is the same as DP and is mostly used in Android/ophone examples.
    Device Independent pixels (device independent pixels). Different devices have different display effects. This is related to the hardware of the device. We recommend that you use this feature to support WVGA, hvga, and qvga, without relying on pixels.
  • SP (pixels irrelevant to the scale): similar to DP, but can be scaled based on the user's font size preferences.
    Scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in fonts.

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. The default font size of Android is also sp.

Take dip as the unit of other elements, such as length and height. Of course, you can also consider using a vector image instead of a bitmap.

 

DP is not related to density. SP is not only related to density, but also to scale.

If the screen density is 160, DP and SP are the same as PX. 1dp = 1sp = 1px, but if PX is used as the unit, if the screen size remains unchanged (assuming it is still 3.2), the screen density is changed to 320.

The original textview width is set to 320 PX, And the 3.2-inch screen with a density of 160 is half shorter than the 3.2-inch screen with a density.

But if it is set to 160dp or 160sp. The system automatically sets the width property value to 320px.

That is, 160*320/160. Among them, 320/160 can be called the density proportion factor. That is to say, if DP and SP are used, the system will automatically convert according to the screen density change.

 

References:

Difference between DIP, DP, PX, and SP
Http://sifutian.iteye.com/blog/680935

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.