Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify spacing between elements of LinearLayout only once?

Tags:

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.

like image 902
Dude Avatar asked Sep 17 '12 07:09

Dude


People also ask

How to divide LinearLayout in android equally?

Equal distribution To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

Is it possible to evenly distribute buttons across the width of an android LinearLayout?

Expanding on fedj's answer, if you set layout_width to 0dp and set the layout_weight for each of the buttons to 1, the available width will be shared equally between the buttons.

Which is better LinearLayout or RelativeLayout?

LinearLayout is less used as compared to RelativeLayout. RelativeLayout is used more in applications. We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.

How can I add space between two views in android?

Use the setPadding(left, top, right, bottom) method on the view to set the padding.


1 Answers

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

android:style="@style/mystyle"  <style name="mystyle">       <item name="android:layout_marginTop">10dp</item>       ... other things that your elements have in common </style> 
like image 180
chris-tulip Avatar answered Oct 04 '22 06:10

chris-tulip