Search code examples
androidviewmargin

Android view support margin?


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?


Solution

  • 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:

    http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/view/View.java#15156

    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

    Layouting: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/widget/LinearLayout.java#1539