3

I was wondering if it's possible to set an automatic/dynamic margin (padding?) between elements in an Android layout without having to do it programmatically?

For example let's say there is a horizontal LinearLayout which is set to android:layout_width="fill_parent" and that contains five elements. Is there a setting that evenly shares the remaining empty space of the LinearLayout to the margins of the child elements?

See image at http://img63.imageshack.us/img63/8/margin.png

Thanks for any help!

Sven Jacobs
  • 6,278
  • 5
  • 33
  • 39

2 Answers2

1

You could use view spacers as your margin, with the layout weight set on them.

<LinearLayout ...>
    <View id=marginLeft android:layout_weight="1"/>

    <Element1/>
    <Element2/>
    <Element3/>
    <Element4/>
    <Element5/>

    <View id=marginRight android:layout_weight="1"/>
</LinearLayout>

This should make the two views use up any remaining space in your row. Note, the above XML will not parse :)

-- Edit

Just saw the picture. To add equal spacing between each of the elements too would just be a case of adding more spacer elements between your content elements (all with the same layout weight)

ataulm
  • 15,195
  • 7
  • 50
  • 92
0

Yup. Only LinearLayouts support it. Tis called layout weight

Look for LinearLayout at http://developer.android.com/guide/topics/ui/layout-objects.html

For a good intro, look here

Hamy
  • 20,662
  • 15
  • 74
  • 102
  • Can you please give an example of the layout XML? I thought the weight attribute gives some of the remaining space to an element. I was talking about the margins though. – Sven Jacobs Aug 10 '10 at 10:43