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?
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" .
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With