Android 初学者的 padding 和 margin 区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21959050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android beginner difference between padding and margin
提问by Kashif Nazar
I have referred questions on SO. ALso checked an answer:
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).
我已经提到了关于 SO 的问题。还检查了一个答案:
填充是边框内的空间,在边框和实际视图的内容之间。请注意,填充完全围绕内容:顶部、底部、右侧和左侧都有填充(可以是独立的)。
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.
边距是边框外、边框与此视图旁边的其他元素之间的空间。在图像中,边距是整个对象之外的灰色区域。请注意,与填充一样,边距完全围绕内容:顶部、底部、右侧和左侧都有边距。
Also, more on padding and margins from:
此外,更多关于填充和边距的信息来自:
http://developer.android.com/reference/android/view/View.html
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
http://developer.android.com/reference/android/view/View.html
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
But what is the difference fundamentally between padding and margins ? Would the behaviour differ depending on O.S. and devices?
I have a normal, simple layout. No problem with code, have used layout folders - layout and layout-sw600dp plus drawables-4dpi. Can't make layout without margin or padding, which one is more appropriate?
但是 padding 和 margin 之间的根本区别是什么?行为会因操作系统和设备而异吗?
我有一个正常的,简单的布局。代码没问题,使用过布局文件夹 - layout 和 layout-sw600dp 加上 drawables-4dpi。不能做没有边距或填充的布局,哪个更合适?
采纳答案by Pararth
Padding
is for inside/within components. Eg. TextView
, Button
, EditText
etc.
Eg. space between the Text and Border
Padding
用于内部/内部组件。例如。TextView
,Button
,EditText
等
如。文本和边框之间的空间
Margin
is to be applied for the on-outside of the components.
Eg. space between left edge of the screen and border of your component
Margin
应用于组件的内外。
例如。屏幕左边缘和组件边框之间的空间
Visual representation is great in : Difference between a View's Padding and Margin
视觉表示在以下方面很棒:视图的填充和边距之间的差异
With Padding
, i have seen a difference in 2.2, 2.3 and say 4.3, 4.4
in such cases:
用Padding
,我已经看到在2.2,2.3的差,并说4.3,4.4
在这种情况下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="ASDFGHJKL" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:text="@string/hello_world" />
</RelativeLayout>
Also, check the use of dimens:
http://developer.android.com/guide/topics/resources/more-resources.html
另外,检查尺寸的使用:http:
//developer.android.com/guide/topics/resources/more-resources.html
回答by Kashif Nazar
Margin
利润
Margins make up the vertical and horizontal areas between elements. If elements have no margins around them, they will bump right up against each other. In other words, he space outside of, or between, elements is what comprises the margin areas.
边距构成元素之间的垂直和水平区域。如果元素周围没有边距,它们就会相互碰撞。换句话说,元素外部或之间的空间构成了边缘区域。
Padding
填充
The padding of an element is the horizontal and vertical space that's set around the content area of the targeted element. So padding is on the inside of a box, not the outside.
元素的内边距是围绕目标元素的内容区域设置的水平和垂直空间。所以填充是在盒子的里面,而不是外面。
回答by Jawad Zeb
In Simple words .. if you want to take your widget like TextView, EditText far away from other. You should use margin from top,right,left,bottom.
用简单的话来说.. 如果你想让你的小部件像 TextView、EditText 远离其他。您应该使用顶部、右侧、左侧、底部的边距。
By increasing padding it will increase the inner spacing not making the widget far apart from others..
通过增加填充,它将增加内部间距,而不会使小部件与其他小部件相距甚远。
Like 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.
例如,像按钮一样,特征按钮背景图像包括填充,但不包括边距。换句话说,添加更多的内边距会使按钮在视觉上看起来更大,而添加更多的边距只会使按钮和下一个控件之间的间隙变大。
回答by Orhan Obut
Margin:Between border and its parent layout
边距:边框与其父布局之间
Padding:Between content and border
填充:在内容和边框之间
回答by Eugene Brusov
The difference between margin and padding and use cases are clearly explained by +Nick Butcherin Udacity's video. Here's the excerpt:
+Nick Butcher在 Udacity 的视频中清楚地解释了边距和填充以及用例之间的区别。这是摘录:
...if you want the touchable area or the background of the objectto be enlarged, then use padding, otherwise use margin...
...如果要放大对象的可触摸区域或背景,请使用填充,否则使用边距...
回答by Sagar Shah
what is the difference fundamentally between padding and margins ?
填充和边距之间的根本区别是什么?
For the differences - Rohan Khandwal has shared a very perfect link.
对于差异 - Rohan Khandwal 分享了一个非常完美的链接。
Would the behaviour differ depending on O.S. and devices?
行为会因操作系统和设备而异吗?
Now If we are talking about the behaviourof the view which has been given diffrent margins & padding. Then It will definitely look diffrent in different devices with diffrent resolutions. Thats why we are given diffrent dimen/values/layout folders which have their own meanings.
现在,如果我们谈论的是具有不同边距和填充的视图的行为。那么它肯定会在具有不同分辨率的不同设备中看起来不同。这就是为什么我们会得到不同的维度/值/布局文件夹,这些文件夹有自己的含义。
回答by Akta Dadhaniya
The padding is expressed in pixels for the left, top, right, and bottom parts of the view. Padding can be used to offset the content of the view by a specific amount of pixels.
视图的左侧、顶部、右侧和底部部分的填充以像素表示。填充可用于将视图的内容偏移特定数量的像素。
For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.
例如,左内边距为 2 会将视图的内容向左边缘的右侧推送 2 个像素。
Padding can be set using the setPadding(int, int, int, int)
method and queried by calling getPaddingLeft()
, getPaddingTop()
, getPaddingRight()
, and getPaddingBottom()
.
填充可以通过设置setPadding(int, int, int, int)
方法调用和查询getPaddingLeft()
,getPaddingTop()
,getPaddingRight()
,和getPaddingBottom()
。
Margins are the spaces outside the border, between the border and the other elements next to this view. Note that, like the padding, the margin goes completely around the content :there are margins on the top, bottom, right, and left sides. Margin can be set using the setMargins(int left, int top, int right, int bottom) method.
边距是边框外、边框与此视图旁边的其他元素之间的空间。请注意,与填充一样,边距完全围绕内容:顶部、底部、右侧和左侧都有边距。可以使用 setMargins(int left, int top, int right, int bottom) 方法设置边距。
回答by Samarth Shah
The difference between android margin and padding is that even though the text is how much sp you want away from the edge, margin is not spaced or colored in. It is only the text and the color you set with it all "alone". With padding though, the text is away from the edge of the screen just like margin but, in padding the text is away and all the space between the text and the edge of the screen is filled in with the color or any text preference you chose to be. This is the difference between android margin and android padding.
android margin 和 padding 之间的区别在于,即使文本是你想要远离边缘的 sp 多少,margin 没有间隔或着色。它只是文本和你用它“单独”设置的颜色。虽然使用填充,文本远离屏幕边缘,就像边距一样,但是,在填充文本时,文本和屏幕边缘之间的所有空间都填充有您选择的颜色或任何文本首选项成为。这就是android margin和android padding的区别。
回答by Vivek Nayak
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.
边距是边框外、边框与此视图旁边的其他元素之间的空间。在图像中,边距是整个对象之外的灰色区域。请注意,与填充一样,边距完全围绕内容:顶部、底部、右侧和左侧都有边距。
Padding Increases the size of the view where as margin doesn't because it is outside the view
Padding 增加视图的大小,因为边距不在视图之外