How to set equal spacing with Android GridView?
Asked Answered
U

2

7

How to set the space left over to be divided equally?

For example, I have a GridView with 2 columns. The full width is 700px, and the column is 200px: how do I specify the xml, so the space left (300px) are divided evenly into 3 pieces?

i.e. 100px | 200px (Column 1) | 100px | 200px (Column 2) | 100px

Uranology answered 22/10, 2013 at 23:17 Comment(0)
L
13

Set the android:stretchMode to spacingWidthUniform.

http://developer.android.com/reference/android/widget/GridView.html#attr_android:stretchMode

Lesbianism answered 22/10, 2013 at 23:20 Comment(1)
May seem obvious (it wasn't to me), but you need to remove any android:horizontalSpacing you have otherwise it will not work correctlyFolliculin
K
1

Alternatively you could use android.support.v7.widget.GridLayout as such

<android.support.v7.widget.GridLayout app:columnCount="5">
    <Space app:layout_columnWeight="1> 
    <View>
    <Space app:layout_columnWeight="1"/> 
    <View>
    <Space app:layout_columnWeight="1>
</android.support.v7.widget.GridLayout>

to achieve the desired effect, unless you are building exclusively for SDK 21, and then use the normal GridLayout

Kraska answered 22/1, 2015 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.