2

The combination of having, for example, two buttons in some layout with android:layout_width="0dip" android:layout_weight="1" properties works just fine, both buttons will occupy equal space!

But what if I want to have 2 buttons but also a space for one more, and that space to be equal like the space the buttons will occupies.

Put in other words, I want to have a buttons with android:layout_width="0dip" android:layout_weight="1" properties and a space on the right of the buttons that will act just like invisible button with android:layout_width="0dip" android:layout_weight="1" properties

Is there a clean way to do this ?

I do not want to do hacks like textview with no text on it and android:layout_width="0dip" android:layout_weight="1" properties ...

I want to know is there way to put 'weight' on the margins or to the paddings. I think there is no such a thing but I want to hear your opinion and your suggestions for this kind of layouts, that shrink depending on the screen width.

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Lukap
  • 31,523
  • 64
  • 157
  • 244

3 Answers3

1

You should look at the weightSum property. If you place the 2 elements in a horizontal LinearLayout, each element with layout_weight=1, declare the weightSum=3 of the LinearLayout, this should result in exactly what you need.

Also, if you decide to hack your way through this, check out a Space view - its a lightweight view for such cases.

George
  • 3,727
  • 9
  • 31
  • 47
0

use android:weightsum=100 in main layout and whatever the size u need for the buttons divide the sum into that number of parts and there in your buttons use android:layout_width=" ". It automatically sets the spacing and the size of the buttons.

SreeRanga
  • 49
  • 3
0

This isn't exactly the answer to your question, but you can set a button to visibility "INVISIBLE" (as opposed to GONE), and it'll be invisible but still occupy the same space.

Otherwise, try making the layout_weight of your button half the weightSum specified in the containing layout and align your button to the left. If it doesn't work, and you don't like the solution in the first paragraph, you can just use a View as a spacer item.

coyotte508
  • 9,175
  • 6
  • 44
  • 63
  • yes I know that I can play with invisible components and put some weights on them, but my question was is there plausibility to put weight on the margin, that it if my element scale I want my margin to scale also... – Lukap Sep 09 '11 at 15:40