Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to do dynamic but square layout

Tags:

android

I'm using a GridView to display a bunch of views which are essentially LinearLayouts. I want the LinearLayouts to all be square, but I also want them to be dynamically sized--that is, there are two columns and I want the LinearLayouts to stretch depending on the size of the screen but remain square. Is there a way to do this through the xml layout or do I have to set the heights and widths programmatically?

like image 1000
Catherine Avatar asked Jan 24 '12 01:01

Catherine


People also ask

How do you create a linear layout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .


1 Answers

A neat solution for square GridView items is to extend RelativeLayout or LinearLayout and override onMeasure like so:

@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     super.onMeasure(widthMeasureSpec, widthMeasureSpec); } 
like image 122
jdamcd Avatar answered Sep 22 '22 22:09

jdamcd