Reputation: 10365
I have 12 basically identical views which I want to arrange in a grid that covers the whole screen. Depending on the device's orientation, I want to use a 3x4 or a 4x3 grid.
As far as I understand, there are basically three approaches to this topic:
GridView
LinearLayout
instancesTableLayout
I'd like to have a layout that
GridView
does)LinearLayout
instances do)GridView
)By default, GridView
has scrolling and doesn't fill the screen, whereas LinearLayout
and TableLayout
don't automatically adapt to orientation changes.
Currently I'm using a GridView
with disabled scrolling and a custom adapter which sets the item views' minimum height depending on the orientation and the container's height to force a filled screen. This works but feels like a really ugly hack.
Dynamically constructing nested LinearLayout
instances depending on the orientation would probably also work, although I haven't tried that.
This seems to be a frequent goal (1, 2, 3, 4), but all the suggested solutions are either as hackish as mine or don't satisfy some of my requirements.
As I'm new to Android development I'm not sure whether I'm missing something.
What is the optimal way of implementing this?
I'm targeting API level 8 and above.
Upvotes: 0
Views: 183
Reputation: 87064
Use a GridView
A GridView
is a widget that you would use when you want to show data in a grid like manner with a larger set of data(as the GridView
's recycling mechanism would provide a greater performance than a normal built hierarchy). This is not your case as you want all the views visible from the start and from my point of view the overhead of a GridView
isn't simply worth it.
Use nested LinearLayout instances
A good option but avoid nested weights. You could use instead two LinearLayout
with weights on the longest direction(vertical for portrait and horizontal for landscape) placed in a RelativeLayout
with a centered anchor view.
Use a TableLayout
Another option. Use the stretchColumns
option for the width and weight
on the TableRows
for the height.
Depending on the device's orientation, I want to use a 3x4 or a 4x3 grid. What is the optimal way of implementing this?
There isn't an optimal way, either of the solutions above could be used, you could also make your own layout.
Upvotes: 1