Android gridview,如何消除网格项之间的间隙
我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
还有另一个可行的解决方案。当从
GridView
适配器中的getView()
方法返回视图时,您必须按如下方式设置布局参数:There is another working solution. When returning the view from the
getView()
method in the adapter for theGridView
, you have to set layout parameters as follows:如果您的适配器中未添加填充,请查看您的 XML。查看 android:stretchMode 以及 android:verticalSpacing 和 android:horizontalSpacing。
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.
或者更简单,在我的单元格的 xml 中,我将以下参数放在根视图上
并像这样设置我的 gridview:
Or even simpler, in the xml of my cell, I put the following parameters on the root view
and setup my gridview like that:
最后我只是计算了屏幕宽度并将其除以列数,并将结果用作一项的宽度。我想要连续。这解决了问题。 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.
如果您查看 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