为何会出现在我的Andr​​oid GridView的额外的像素?我的、出现在、像素、Andr

2023-09-12 07:02:28 作者:孤绝如初见

我在我的Andr​​oid应用程序中有一些ImageViews的GridView控件。我的屏幕上的空间是有限的,我想要的图像占用尽可能多的可用空间,因为他们可以。不幸的是,在GridView总会留下5像素的周围ImageViews的外缘空屏幕空间(ImageViews之间的空间设置有水平/垂直间距和行为正确)。空空间的作用有点像周围的ImageViews保证金,但我不能摆脱它。有谁知道是什么导致了这种边界,我怎么能摆脱它(或者至少使其更小)?谢谢你。

I have a GridView in my Android application that has a number of ImageViews in it. 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). The empty space acts kind of like a margin around the ImageViews, but I can't get rid of it. Does anyone know what's causing this "border" and how I can get rid of it (or at least make it smaller)? Thanks.

更新:我在我的适配器类的getView()方法膨胀的.xml文件创建ImageViews。这里的XML我膨胀:

Update: I'm creating the ImageViews by inflating an .xml file in the getView() method of my Adapter class. Here's the xml I'm inflating:

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

我定义的GridView控件像这样我的布局xml文件:

I defined the GridView in my layout xml file like this:

<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"/>

下面是一个屏幕截图的问题。红色区域在我的GridView。紫色区域是我ImageViews。所显示的图像是一个简单的蓝色矩形,一个透明的中心。该图像是45X45像素(但只有30×30在我的应用程序像素 - 我会担心以后)。围绕着紫色的红色边框就是我试图消除。

Here's a screen shot of the problem. The red area in my GridView. The purple areas are my 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). The red border around the purple is what I am trying to eliminate.

推荐答案

谢谢你的建议每个人,但事实证明,答案是什么地方,我们没有想到的。额外的像素是由被分配给我的GridView的选择造成的。我想默认的选择是9片图像的周围有5像素的边框。当我创建并分配一个9补丁有更小的边界(或没有边界可言 - 只是一个普通的PNG文件)。在我的GridView额外的空间走了

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. I guess the default selector is a 9-patch image that has a 5 pixel border around it. 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.