如何在Android中移除GridView行之间的空格

19
在Android的网格视图中,网格视图的行之间存在额外的空间。我没有向网格视图添加任何空间。
这是我的XML文件。
        <GridView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/grid"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:horizontalSpacing="5px"
                android:numColumns="3"
                android:columnWidth="50dip"
                 android:verticalSpacing="0dip"
                android:gravity="center"
                />

同时,用于向网格视图添加视图的另一个XML是:

        <ImageView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/singlePhoto"
            android:layout_gravity="top"
            android:layout_width="145dip"
            android:layout_height="145dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            >
        </ImageView>

在适配器类中,代码如下:

        if(mView == null){
            mView = mLayoutInflater.inflate(R.layout.newsgridthumbpic, null);
                }

                ImageView mImage = (ImageView)mView.findViewById(R.id.singlePhoto);
                PhotoGridInfoSet mInfo = (PhotoGridInfoSet)details.get(position);
                if(mImage != null){
                mImage.setTag(mInfo.getPhotoThumbNailString());
                mImage.setVerticalScrollBarEnabled(true);
                mImage.setVerticalFadingEdgeEnabled(true);
                }
        }!

表格视图中的空格是在行之间。我在代码中没有添加任何空格,但仍然存在垂直间距。我不知道为什么表格视图行之间会有空格。在hdpi设备/模拟器中不会出现这个问题,但在mdpi和ldpi设备/模拟器中会出现。

参考图像。目前的情况就是这样。

8个回答

43

我解决了这个问题。

为了解决它,我需要在我的xml文件中添加一行代码。

android:adjustViewBounds="true"

添加这个代码后,现在不会出现空格了。


你是将它添加到了GridView还是GridView的子项中呢?它在我的电脑上好像不起作用 :( - LairdPleng
10
将此代码添加到ImageView中,这对我有用...谢谢Dipali。 - Anchit Mittal
重要提示:就像Anchit提到的那样,这应该放在ImageView中。 - JY2k
嗨,Dipali,这对我不起作用...你能告诉我如何解决这个问题吗?在我的情况下,我有2列,我不想在图像之间留任何空格... - Rishi
但是在XML文件中的哪里?在GridView还是其他地方? - Jayesh
显示剩余2条评论

2

只需将最后两行放入网格视图中即可。

<GridView
        android:numColumns="2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/gridView"
        android:horizontalSpacing="0dip"
        android:verticalSpacing="0dip"/>

0
将以下代码行放入您的getView()方法中...
mImage.setPadding(0, 0, 0, 0);

希望这能对你有所帮助。


0
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:horizontalSpacing="5px"
    android:numColumns="3"
    android:columnWidth="50dip"
     android:verticalSpacing="0dip"
    android:gravity="center"/>

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/singlePhoto"
    android:layout_width="145dip"
    android:layout_height="145dip"
    android:scaleType="fitXY">
</ImageView>

请提供一个简要的解释,而不仅仅是代码。 - Jan
android:scaleType="fitXY" 是帮助解决问题的属性。 - arun8

0

将gridview中的android:verticalSpacing设置为负值,可以去除垂直间距。

(例如:android:verticalSpacing="-10dp")

测试一些数字以获取正确的值。


0

看起来你的图片正在按比例缩放以适应50dp的列宽。这会在垂直布局中引入大量未填充的空间。如果你想让你的列宽为50dp,最简单的解决方法是调整你的ImageView宽度为50dp,并按比例调整ImageView的高度。


即使我将其更改为dip,它仍然显示行之间的空格,问题在于垂直间距。 - Dipali
你放置在网格中的视图的高度定义了网格的高度。这与你的重力设置为“顶部”混合在一起,导致你的网格看起来像那样。 - Nick Campion

0

请检查您的"newsgridthumbpic"布局是否包含额外的填充或任何具有大高度的背景图像,以适应您的网格视图元素。

还要仔细检查您是否将column_width设置为50dip,但您的"imageview's"宽度为145 dip


是的,我已经尝试过改变ImageView的高度和宽度等所有这些事情,而这些图片都来自服务器,所以没有背景或src图像。 - Dipali

0

我还有另一种解决使用 RecyclerView 时相同问题的方法。

只需像这样传递 SpanCount 的数字,例如 GridLayoutManager(this, 6) 中的 6,并更改 SpanCount 以减少或增加 RecyclerView 中项目之间的间隔。

请查看此示例代码-

GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 6);
  rvSizes.setLayoutManager(gridLayoutManager);
  SizeSelectorAdaptersizeChooserAdapter = new SizeSelectorAdapter(this, sizes);
  rvSizes.setAdapter(sizeChooserAdapter);

希望这对你有用。 谢谢。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接