41

Setting two or more elements of a linear layout the same height seems to be a great problem.

I want to set four buttons in a row to the same height.

android:layout_height="wrap_content" does it for the moment but when the text on one of the buttons is longer than one line this button is increased and therefore bigger than the other ones. Due to different localisations I don't know, when and which button may have a second line.

So my idea is, to set the parent linearlayout to android:layout_height="wrap_content" and all (!) child heights to android:layout_height="fill_parent".

This works (all buttons have the same size), but I'm not sure if this causes any other problems? Because it the parent gets it's height from the childs and vice-versa.

nob
  • 1,067
  • 1
  • 11
  • 18

2 Answers2

58

In theory what you are describing should not work ("Because it the parent gets it's height from the childs and vice-versa".) However, we made it work in LinearLayout because it was a very common use case. I recently added similar support to FrameLayout (this feature should be part of Honeycomb.) What you are doing is therefore perfectly valid and will work just fine.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 4
    Even when accepting that it works, I'm still confused as to why it works. It's a bit weird because of the recursive nature (parent asks child and child asks back). So is there an actual documentation somewhere stating about how the layout process works in this case ? In particular, the parent choses its size to be the size of the largest child. Hence the result of all childs with the same size. Is this documented ? – ramcrys Sep 09 '13 at 13:18
  • @ramcrys it might help to remember that the child never "asks back" for the parents size - it's always given one: the parents measure the children, passing the available size (pixels) with a qualifier (unspecified, at most, exactly). – ataulm Aug 03 '15 at 15:01
  • this doesn't seem to `wrap_content` very strongly. I've got two buttons sde by side in a `LinearLayout`. They are taller with their height set to `match_parent` than with `wrap_content`. EDIT: ah, `LinearLayout` is also having some fun with `baseLineAligned` – Sam Sep 29 '15 at 11:06
-2

That doesn't make sense :(

Why don't you use android:singleLine="true" and some ellipsode?

Macarse
  • 91,829
  • 44
  • 175
  • 230