Android gridview,如何消除网格项之间的间隙

发布于 2024-12-07 13:45:46 字数 322 浏览 2 评论 0原文

我正在尝试使用GridView来实现马赛克效果。网格项都是图像,类似于 APIDemo 示例中的 Gri 2。问题是图像之间总是存在间隙,即使所有填充和间距都设置为 0 后也是如此。我是否错过了一些其他设置来消除间隙?我希望图像彼此相邻。

我将列宽设置为 120dip。这是我将图像宽度设置为 120dip 的代码:

mTileWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, r.getDisplayMetrics());

有问题吗?

谢谢!

I am trying to use GridView to achieve a mosaic effect. The grid items are all images, similar to the Gri 2 in the APIDemo sample. The problem is that there are always gaps between the images, even after all the padding and spacing are set to 0. Did I miss some other settings to remove the gap? I want the images right next to each other.

I set the column width as 120dip. Here is my code to set the image width to 120dip:

mTileWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, r.getDisplayMetrics());

Is there a problem?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海不蓝 2024-12-14 13:45:47

还有另一个可行的解决方案。当从 GridView 适配器中的 getView() 方法返回视图时,您必须按如下方式设置布局参数:

view.setLayoutParams(new GridView.LayoutParams(
    TableLayout.LayoutParams.MATCH_PARENT, 
    TableLayout.LayoutParams.MATCH_PARENT));

There is another working solution. When returning the view from the getView() method in the adapter for the GridView, you have to set layout parameters as follows:

view.setLayoutParams(new GridView.LayoutParams(
    TableLayout.LayoutParams.MATCH_PARENT, 
    TableLayout.LayoutParams.MATCH_PARENT));
幻想少年梦 2024-12-14 13:45:47

如果您的适配器中未添加填充,请查看您的 XML。查看 android:stretchMode 以及 android:verticalSpacing 和 android:horizo​​ntalSpacing。

    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/gridview"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:columnWidth="14dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
    />

If the padding isn't added in your adapter, look at your XML. Look at android:stretchMode as well as android:verticalSpacing and android:horizontalSpacing.

    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/gridview"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:columnWidth="14dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
    />
撩人痒 2024-12-14 13:45:47

或者更简单,在我的单元格的 xml 中,我将以下参数放在根视图上

    android:layout_width="match_parent"
    android:layout_height="match_parent"

并像这样设置我的 gridview:

   <GridView
    android:id="@+id/gridview_type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchMode="columnWidth"
    android:numColumns="3"
    tools:listitem="@layout/cell_type"
    />

Or even simpler, in the xml of my cell, I put the following parameters on the root view

    android:layout_width="match_parent"
    android:layout_height="match_parent"

and setup my gridview like that:

   <GridView
    android:id="@+id/gridview_type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchMode="columnWidth"
    android:numColumns="3"
    tools:listitem="@layout/cell_type"
    />
妄司 2024-12-14 13:45:46

最后我只是计算了屏幕宽度并将其除以列数,并将结果用作一项的宽度。我想要连续。这解决了问题。 mmmmm5的答案也许是正确的,间隙是android添加的填充,我们只需要绕过它即可。

In the end I just calculated the screen width and divided it by the number of columns, and use the result as the width for one item. I want in a row. That fixed the problem. The answer by mmmmm5 perhaps is correct that the gap is the padding added by android, we just need to get around it.

或十年 2024-12-14 13:45:46

如果您查看 Android 的联系人源代码,就会创建一个自定义 GridView 类。快速回顾一下,有填充等设置。也许,这回答了你的问题

If you look at Android's source code for Contacts, a custom GridView class is created. Reviewing quickly, there are settings to paddings, etc. Perhaps, that answers your question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文