Margin does not impact in "include"
I had the same problem and the answer from Kamen Goranchev doesn't work for me.
I have used ADT's feature "Extract include..." from the layout editor to extract some commonly used badges as a list of TextView-elements. So the Extract-include-tool wrapped my TextView-Elements in a merge-tag, which usually would be fine.
But, according to the very helpful sourcecode-link from boiledwater I see in line 888 https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/LayoutInflater.java#L888, the layout-attributes from the include-tag itself are only parsed if the include doesn't have the merge-tag as its root-element.
So I removed the merge-tag from the include and used another ViewGroup-tag like e.g. FrameLayout. Then the margins in the include-tag are working as expected.
You should add the android:layout_width
and android:layout_height
attributes in the include
tag. Otherwise, the margins are not taken into consideration.
However, if you want to override layout attributes using the
<include>
tag, you must override bothandroid:layout_height
andandroid:layout_width
in order for other layout attributes to take effect.
https://developer.android.com/training/improving-layouts/reusing-layouts.html#Include
include
tag support below properties:
Any android:
layout_*
attributes which you can overwrite.android:id
attribute.layout
attribute.android:visibility
attribute.
Etc:
include android:id=”@+id/news_title”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
layout=”@layout/title”/>
Please read:
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/LayoutInflater.java#L777
http://developer.android.com/training/improving-layouts/reusing-layouts.html