How to get rid of spaces in gridview

被刻印的时光 ゝ 提交于 2019-12-02 23:29:28

If not otherwise set, a GridView will override the layout you're imagining it should produce by stretching things in various ways. If you want to have full control of the layout by defining the column widths and the size of the grid items, then you have to make sure to set the GridView's stretchMode property to "none".

<GridView
        ...
        android:stretchMode="none">
</GridView>

Also, you are filling the parent with your GridView width. I think you should be wrapping the content for the width unless you want extra filling added. Where are you adding the 5dp padding? Around each bitmap or around the edge of the gridview? I don't see it.

You should probably be filling the height though. (in the following some of the width and heights can probably be replaced by "wrap_content")

So, for you

<?xml version="1.0" encoding="utf-8"?>
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageGrid"
    android:stretchMode="none"        
    android:layout_width="XXXdp"
    android:layout_height="fill_content"
    android:numColumns="6"
    />

Also, not sure if you need column spacing and width, which would be something like (or a dp instead of wrap_content) - you may not need these in your GridView - I'm not sure:

    android:columnWidth="YYYdp"
    android:horizontalSpacing="0dp"

ImageViews with 5dp padding:

<?xml version="1.0" encoding="utf-8"?>
<ImageView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/grid_item_image"
   android:padding="5dp"
   android:layout_width="UUUdp"
   android:layout_height="VVVdp" />
Paul

For me it solved the problem by setting imageview.setAdjustViewBounds(true); in my Adapter class.

It seems that my square images were automatically resized in width but Gridview still used the original hight. By adjusting bounds, both dimensions were resized automatically and all items were displayed smoothly even with default stretchmode enabled.

hydra

I found an easy way to walk around this. Just add android:layout_marginRight in your GridView.

Helin Wang

step 1:

android:stretchMode="none"
android:numColumns="auto_fit"
android:gravity="center"

step 2: in onMeasure return your calculated width and height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!