The difference between android:padding and Android:margin

Source: Internet
Author: User

This article synthesizes: http://zhujiao.iteye.com/blog/1856980 and http://blog.csdn.net/maikol/article/details/6048647 two articles

Have added some of their own experience.

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

A popular understanding:

Padding is an inner border that refers to the inner content of the control, such as the margins of the text/picture distance from the control

Margin is an outer border that refers to the margin of the control from the edge parent control

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 left and right are the same settings, you can set the

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

When the buttons set the above two properties separately, the effect is different.

Android:paddingleft= "30px":

The content set on the button (the case) is 30 pixels from the left edge of the button.

android:layout_marginleft= "30px"

The entire button is set from the left side of the content 30 pixels

These two properties are relative, assuming B is a child control, setting B's margin and setting A's padding can achieve the same effect.

Benefits of setting padding:

if the imageview corresponding picture is small, click Not easy to point, by increasing the padding can increase the touch sensitivity

The length units supported by Android.

    • PX (pixels): The point on the screen.
      pixels (pixels). Different devices show the same effect, generally we hvga represent 320x480 pixels, this use more.
    • In (inches): unit of length.
    • MM (mm): unit of length.
    • PT (lb): 1/72 inches.
      Point, is a standard length unit, 1pt=1/72 inches, for printing, very simple to use;
    • DP (density-independent pixels): An abstract unit based on screen density. 1DP = 1px on a monitor 160 dots per inch.
    • Dip: Same as DP, more for android/ophone example.
      device independent pixels (unit independent pixels). Different devices have different display effects, this is related to the device hardware, generally we support WVGA, HVGA and QVGA recommend this, do not rely on pixels.
    • SP (scale independent pixels): Similar to DP, but can be scaled based on the user's font size preference.
      scaled pixels (enlarge pixels). Mainly used for font display of best for textsize.

In order for the user interface to display properly on the current and future display types, it is recommended that you always use the SP as the text size unit, and the Android default font size is also used by the SP.

The dip is used as a unit of other elements, such as length and height. Of course, you can also consider using vector graphics instead of bitmaps.

The DP is not density-independent, and the SP is not related to scale, except for density.

If the screen density is 160, then the DP and SP and PX are the same. 1DP=1SP=1PX, but if you use PX as the unit, if the screen size is constant (assuming 3.2 inches), the screen density becomes 320.

So the original width of the TextView is set to 160px, in the density of 320 of the 3.2-inch screen is more than the density of 160 of the 3.2-inch screen to see the half-short.

But if it is set to 160DP or 160SP. The Width property value is automatically set to 320px.

That means 160 * 320/160. 320/160 of these are called density scale factors. That is, if you use the DP and SP, the system will automatically convert based on the change in screen density.

The difference between android:padding and Android:margin

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.