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

Why are there extra pixels around my Android GridView?(为什么我的 Android GridView 周围有多余的像素?)

本文介绍了为什么我的 Android GridView 周围有多余的像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Android 应用程序中有一个 GridView,其中包含许多 ImageView.我屏幕上的空间有限,我希望图像尽可能多地占用可用空间.不幸的是,GridView 总是在 ImageViews 的外边缘留下 5 个像素的空白屏幕空间(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.

更新:我通过在我的 Adapter 类的 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" />

我在我的布局 xml 文件中定义了 GridView,如下所示:

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 像素(但在我的应用程序中只有 30x30 像素 - 我稍后会担心).紫色周围的红色边框是我要消除的.

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-patch 图像,它周围有一个 5 像素的边框.当我创建并分配一个具有较小边框(或根本没有边框 - 只是一个普通的 .png 文件)的 9-patch 时,我的 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.

这篇关于为什么我的 Android GridView 周围有多余的像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么我的 Android GridView 周围有多余的像素?

基础教程推荐