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

Johnny
  • 2,800
  • 6
  • 25
  • 33

2 Answers2

13

Set the android:stretchMode to spacingWidthUniform.

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

Lesleh
  • 1,665
  • 15
  • 24
  • 4
    May seem obvious (it wasn't to me), but you need to remove any `android:horizontalSpacing` you have otherwise it will not work correctly – Joe Maher Nov 25 '15 at 00:37
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

Community
  • 1
  • 1
AllDayAmazing
  • 2,383
  • 1
  • 24
  • 25