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.

2
  • I had the exact same question, thank you very much for asking it sir :) Commented Nov 16, 2011 at 12:01
  • Am I the only one in which this is not working? Commented Mar 28, 2016 at 22:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 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).
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
-2

That doesn't make sense :(

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.