The difference between android:padding and android:margin

巴扎黑
Release: 2017-06-28 14:27:43
Original
1460 people have browsed it

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

Added some of my own experiences.

Android’s Margin and Padding are the same as those of Html. As shown in the figure below: the yellow part is Padding and the gray part is Margin.

The difference between android:padding and android:margin

Popular understanding:

Padding is the inner border, refers to the control internal content, such as text /The margin of the picture from the control

Margin is the outer border, refers to the margin of the control from the 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 the left, right, top and bottom settings are the same, you can set them directly

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


When the button sets the above two attributes respectively, the effect obtained are different.

android:paddingLeft="30px":

The content (such as pictures) set on the button is 30 pixels from the left border of the button.

android:layout_marginLeft="30px"

The entire button is 30 pixels away from the content set on the left

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

Benefits of setting padding:

If the picture corresponding to the The difference between android:padding and android:marginview is relatively small and it is difficult to click on it, the touch sensitivity can be increased by adding padding


Android supported length units.

  • px (pixel): point on the screen.
    pixels (pixels). Different devices have the same display effect. Generally, our HVGA represents 320x480 pixels, which is more commonly used.

  • in (inch): unit of length.

  • mm (millimeters): unit of length.

  • pt (pounds): 1/72 inch.
    point is a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very simple and easy to use;

  • dp (density-independent pixel): An abstract unit based on screen density. On a 160 dots per inch monitor, 1dp = 1px.

  • dip: Same as dp, mostly used in android/ophone examples.
    device independent pixels. Different devices have different display effects. This is related to the device hardware. Generally, in order to support WVGA, HVGA and QVGA, we recommend using this, which does not rely on pixels.

  • sp (scale-independent pixels): Similar to dp, but can be scaled based on the user's font size preferences.
    scaled pixels (enlarged pixels). Mainly used for font display best for textsize.

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

Use dip as the unit of other elements, such as length and height. Of course, you can also consider using vector graphics shapes instead of bitmaps.

dp has nothing to do with density. In addition to having nothing to do with density, sp has nothing to do with scale.

If the screen density is 160, then dp, sp and px are the same. 1dp=1sp=1px, but if you use px as the unit, if the screen size remains unchanged (assuming it is still 3.2 inches), the screen density becomes 320.

Then the width of the original TextView is set to 160px, which is half shorter when viewed on a 3.2-inch screen with a density of 320 than on a 3.2-inch screen with a density of 160.

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

That is 160 * 320 / 160. Where 320/160 can be called the density scaling factor. In other words, if you use dp and sp, the system will automatically convert according to changes in screen density.

The above is the detailed content of The difference between android:padding and android:margin. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!