views:

58

answers:

1

Hi all,

I am trying to build a layout dynamically which display some text and image for the most part, but has a series of buttons placed next to each other in the bottom.

I have a linear layout that carries the text, another linear layout that carries the image. And yet another linear layout that carries the buttons that get created in a for loop. I have a main layout aligned vertical that adds the text, image and buttons layout, in that order. To finally generate something like this:

Text .... Image ... Button1 Button2 Button3....

The problem is the number of buttons get decided at runtime, so if there are more than 4 buttons, the 5th button gets displayed really tiny. Also, when I tilt the phone, I get only the text and image showing, but no buttons coz the image covers the entire screen.

Layoutting seems to be pretty complicated to me, any help is appreciated!

Thanks George

A: 

You do not need to wrap single views in a linear layout, so add the text and image directly to the root linear layout. You might consider using a relative layout instead of linear for the root.

Using FILL_PARENT and WRAP_CONTENT for the LayoutParams width or height can give some useful results. For example, using FILL_PARENT for the image height might scale it down to leave room for the buttons.

Be careful with LayoutParams because there are lots of them and only the one that matches the ViewGroup class should be used.

One option would be to implement an onLayout method of your own in a custom ViewGroup. You will be passed the dimensions you have to work with and be able to position all the views as you see fit.

drawnonward
Thanks for the reply. I am not sure I understand "Be careful with LayoutParams because there are lots of them and only the one that matches the ViewGroup class should be used". Can you give an example?Any code snippets with onLayout will be helpful!
Chris