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:

Dip, dp, px, sp difference: html "> http://www.bkjia.com/kf/201104/88848.html

 

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.