Difference between a View's Padding and Margin

2018-12-31 08:56发布

What is the difference between a View's Margin and Padding?

14条回答
何处买醉
2楼-- · 2018-12-31 09:15

Sometimes you can achieve the same result by playing only with padding OR margin. Example :

Say View X contains view Y (aka : View Y is inside View X).

-View Y with Margin=30 OR View X with Padding=30 will achieve the same result: View Y will have an offset of 30.

查看更多
永恒的永恒
3楼-- · 2018-12-31 09:16

Padding means space between widget and widget original frame. But the margin is space between widget's original frame to boundaries other widget's frame.enter image description here.

查看更多
一个人的天荒地老
4楼-- · 2018-12-31 09:19

In addition to all the correct answers above, one other difference is that padding increases the clickable area of a view, whereas margins do not. This is useful if you have a smallish clickable image but want to make the click handler forgiving.

For eg, see this image of my layout with an ImageView (the Android icon) where I set the paddingBotton to be 100dp (the image is the stock launcher mipmap ic_launcher). With the attached click handler I was able to click way outside and below the image and still register a click.

enter image description here

查看更多
明月照影归
5楼-- · 2018-12-31 09:22

Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).

Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object. Note that, like the padding, the margin goes completely around the content: there are margins on the top, bottom, right, and left sides.

An image says more than 1000 words (extracted from Margin Vs Padding - CSS Properties):

alt text

查看更多
骚的不知所云
6楼-- · 2018-12-31 09:23

In simple words:

  1. Padding - creates space inside the view's border.
  2. Margin - creates space outside the view's border.
查看更多
只靠听说
7楼-- · 2018-12-31 09:25

Padding is the space inside the border between the border and the actual image or cell contents. Margins are the spaces outside the border, between the border and the other elements next to this object.

查看更多
登录 后发表回答