简体   繁体   English

为什么我的Android GridView周围有多余的像素?

[英]Why are there extra pixels around my Android GridView?

I have a GridView in my Android application that has a number of ImageViews in it. 我的Android应用程序中有一个GridView,其中包含许多ImageViews。 The space on my screen is limited and I want the images to take up as much of the available space as they can. 屏幕上的空间有限,我希望图像占用尽可能多的可用空间。 Unfortunately, the GridView always leaves 5 pixels of empty screen space around the outside edge of the ImageViews (the space between ImageViews is set with horizontal/vertical spacing and behaves correctly). 不幸的是,GridView始终在ImageViews的外部边缘周围留有5个像素的空白屏幕空间(ImageViews之间的空间设置为水平/垂直间距,并且行为正确)。 The empty space acts kind of like a margin around the ImageViews, but I can't get rid of it. 空的空间有点像ImageViews周围的空白,但是我无法摆脱它。 Does anyone know what's causing this "border" and how I can get rid of it (or at least make it smaller)? 有谁知道造成这个“边界”的原因以及如何摆脱它(或者至少使其变小)? Thanks. 谢谢。

Update: I'm creating the ImageViews by inflating an .xml file in the getView() method of my Adapter class. 更新:我通过在Adapter类的getView()方法中扩展一个.xml文件来创建ImageViews。 Here's the xml I'm inflating: 这是我要夸大的xml:

<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="#FF00FF" />

I defined the GridView in my layout xml file like this: 我在布局XML文件中定义了GridView,如下所示:

<GridView
   android:id="@+id/mygrid"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_above="@+id/abutton"
   android:layout_marginBottom="8dp"
   android:numColumns="5" android:background="#FF0000"/>

Here's a screen shot of the problem. 这是问题的屏幕快照。 The red area in my GridView. 我的GridView中的红色区域。 The purple areas are my ImageViews. 紫色区域是我的ImageViews。 The image being displayed is a simple blue rectangle with a transparent center. 显示的图像是一个中心透明的简单蓝色矩形。 The image is 45x45 pixels (but is only 30x30 pixels in my app - I'll worry about that later). 图片为45x45像素(但在我的应用中仅为30x30像素-我稍后会担心)。 The red border around the purple is what I am trying to eliminate. 我要消除的是紫色周围的红色边框。

替代文字

Thanks for the advice everyone, but it turns out the answer was somewhere that we didn't expect. 感谢大家的建议,但事实证明答案出在我们意料之外的地方。 The extra pixels were caused by the selector that was assigned to my GridView. 多余的像素是由分配给我的GridView的选择器引起的。 I guess the default selector is a 9-patch image that has a 5 pixel border around it. 我猜默认选择器是一个9色块图像,其周围有5个像素边框。 When I created and assigned a 9-patch that had smaller borders (or no borders at all - just a normal .png file) the extra space in my GridView went away. 当我创建并分配一个具有较小边框(或根本没有边框-只是一个普通的.png文件)的9补丁时,GridView中的多余空间就消失了。

对我来说,除了在xml中设置android:columnWidth 属性 (例如android:columnWidth="50dp" )外,没有任何帮助

您是否尝试将ImageView上的padding以及GridView上的horizontalSpacingverticalSpacing设置为0?

Try setting the layout_height and layout_width of the image view to fill_parent. 尝试将图像视图的layout_height和layout_width设置为fill_parent。

iv.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.MATCH_PARENT, ViewGroup.MATCH_PARENT));

will do it dynamically, I believe. 我相信它将动态地做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM