Which stretchMode to use in GridView on android

2019-04-04 08:16发布

Following is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridviewgallery"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="@dimen/gridview_column_width"
    android:numColumns="auto_fit"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:background="#444444"
/>

When I use android:stretchMode="columnWidth" , it creates a gap between the columns(though it fills the screen width) And if I use android:stretchMode="none" , it gets left aligned and there is a gap left on right side of screen(though now there is no gap between columns) Problem is I want both : To fill the screen width as well no gap between columns. How to achieve it?

3条回答
狗以群分
2楼-- · 2019-04-04 08:37

I know this question is a little old but here is an answer I found hoping this helps someone else, just for android:stretchMode use spacingWidthUniform as it says in documentation here it will stretch the spacing between columns equally.

please refer to above link for more information.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-04-04 08:47

A combination of android:numColumns="auto_fit" android:stretchMode="columnWidth" and android:columnWidth="60dp" will give you a layout that adjusts for different screen sizes. Much better than fixing the number of columns. See example below...

<GridView
        android:id="@+id/tablegrid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffff7965"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginLeft="5dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:columnWidth="60dp"
        />
查看更多
聊天终结者
4楼-- · 2019-04-04 08:49

It's very simple dude, just remove the line

android:columnWidth="@dimen/gridview_column_width"

you can specify any number of columns, i am given number of column = 4

android:numColumns = "4"

This is working fine for me

查看更多
登录 后发表回答