[SOLVED] How to specify spacing between elements of LinearLayout only once?

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.


I recently ran into a problem again that I already had several times in the last years.

LinearLayout is a very convenient layout manager. But what I totally miss is the possibility to add a certain space between the elements (like padding) in a single XML tag.

What I mean by one tag is, that I can define in the declaration of the LinearLayout the spacing between the elements (e.g. in a vertical LinearLayout the vertical space between two elements in this layout).

I know that I can do it by adding the XML tag android:layout_marginTop or something similar to every element in the LinearLayout.

But I would like to be able to define it in only one point, as the spacing is the same for all elements.

Does anybody know an easy way to do this (not implementing a custom LinearLayout or something like that)? I prefer a solution that works directly in XML without the need for coding.


the way that is recommended is to apply a style to all the elements in the linear layout


<style name="mystyle">
      <item name="android:layout_marginTop">10dp</item>
      ... other things that your elements have in common

Answered By – chris-tulip

people found this article helpful. What about you?