Reputation: 1047
From the developer guide it says that
though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support.
but why i can set layout_margin
attributes in ImageView
,EditView
and so on,they exist and work just like padding,
I can't understand what the guide says, Can someone help me to understand it?
Upvotes: 1
Views: 377
Reputation: 107
View class does not contain margins. It contains the padding because padding offset the contents of the view. Margin is meant to offset the view itself from the parent, ViewGroup. As such, the margins are contained in the ViewGroup class. However remember the principle of inheritance. A view is a child of the ViewGroup class. As a result, it inherits the properties of the ViewGroup, including the margins. So when you apply the margin on the view, it responds because it already has the margins properties by virtue of inheritance.
Upvotes: 0
Reputation: 16537
Basically that means that margins are defined in xml for child views, but are used by their parent.
Technically, paddings are fields of the View class. Paddings are being used in the View.draw() method by a View itself. See:
Margins are fields of the MarginLayoutParams class. Margins are used by a ViewGroup to layout its children. See:
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
EDIT:
Margins are loaded to MarginLayoutParams and then used in the layouting phase.
Method which uses these xml attributes to create MarginLayoutParams in FrameLayout: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/widget/FrameLayout.java#678
Loading margins: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/view/ViewGroup.java#6619
Upvotes: 1