What is the difference between a View's Margin and Padding?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
To help me remember the meaning of padding, I think of a big coat with lots of thick cotton padding. I'm inside my coat, but me and my padded coat are together. We're a unit.
But to remember margin, I think of, "Hey, give me some margin!" It's the empty space between me and you. Don't come inside my comfort zone -- my margin.
To make it more clear, here is a picture of padding and margin in a
TextView
:xml layout for the image above
Related
Below image will let you understand the padding and margin-
Padding
Padding is inside of a View.For example if you give
android:paddingLeft=20dp
, then the items inside the view will arrange with20dp
width from left.You can also usepaddingRight
,paddingBottom
,paddingTop
which are to give padding from right, bottom and top respectively.Margin
Margin is outside of a
View
. For example if you giveandroid:marginLeft=20dp
, then the view will be arranged after20dp
from left.Let's just suppose you have a button in a view and the size of the view is 200 by 200, and the size of the button is 50 by 50, and the button title is HT. Now the difference between margin and padding is, you can set the margin of the button in the view, for example 20 from the left, 20 from the top, and padding will adjust the text position in the button or text view etc. for example, padding value is 20 from the left, so it will adjust the position of the text.
Padding is within the view, margin is outside. Padding is available for all views. Depending on the view, there may or may not be a visual difference between padding and margin.
For buttons, for example, the characteristic button background image includes the padding, but not the margin. In other words, adding more padding makes the button look visually bigger, while adding more margin just makes the gap between the button and the next control wider.
For
TextView
s, on the other hand, the visual effect of padding and margin is identical.Whether or not margin is available is determined by the container of the view, not by the view itself. In
LinearLayout
margin is supported, inAbsoluteLayout
(considered obsolete now) - no.Padding is inside of a View.
Margin is outside of a View.
This difference may be relevant to background or size properties.