Android 加载图像时,带背景的ImageView会留下边距

Android 加载图像时,带背景的ImageView会留下边距,android,android-layout,android-imageview,Android,Android Layout,Android Imageview,我的imageview声明如下: <ImageView android:id="@+id/category_image_top" android:layout_width="match_parent" android:layout_height="170dp" android:maxHeight="170dp" android:adjustViewBounds="true" android:sc

我的imageview声明如下:

<ImageView
        android:id="@+id/category_image_top"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:maxHeight="170dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:background="@drawable/image_placeholder"
        />
设置图像时,imageview突然获得4px的边距。但当我从XML中删除android:background=“@drawable/image\u placeholder”时,一切都很好

顺便说一句:如果这有什么区别的话,图像占位符是一个9块的图像

你知道为什么会这样吗

更新:我已经尝试过将背景设置为纯色,然后在加载图像时不会显示任何边距。我还尝试放置另一个9面片图像,当我这样做时,边距再次出现。所以它一定是有背景的图像


更新2:也许这是一个像这家伙指出的安卓错误

我猜图像占位符有4px的透明边距

包含图像视图的元素可能有边距,您检查过了吗

android:background=“@drawable/image\u占位符”
更改为
android:src=“@drawable/image\u占位符”

还添加以下内容:
android:scaleType=“centerCrop”


如果centerCrop不起作用,请尝试列出的其他方法。

确定。找到了解决方案,也许它不是最漂亮的,但嘿,它起作用了!:)

在我的视图中,我刚刚添加了一个任意视图,它是我的图像的保持器。最重要的是,我有我的ImageView,所以当图像加载时,占位符(视图)会被覆盖。这是我的观点(或至少是重要的部分):


...

尝试放置仅带有背景色的父线性布局。与您的图像和应用程序背景不同的东西,例如亮绿色。如果图像具有建议的4px透明边框,则只有在调用setImageBitmap时才会看到绿色边框(我会将其放入按钮调用中,以便您可以根据需要触发它以查找问题)。如果imageview的父级具有边距,则不会看到绿色。这将有助于确定问题所在。老实说,4倍透明的边界更有可能。这就是交易。。如果我在imageview中使用纯色作为背景,那么一切都正常。我尝试了你的建议,但我的背景图像没有透明像素,因此我的线性布局背景无法“穿透”图像视图。听起来你的9修补是错误的。9补丁能用吗?我用过Android 9补丁工具(Draw 9-patch),它看起来不错。图像显示正确,只是干扰了我的src图像。src图像是否应该覆盖所有背景内容?否。。没有边际。。如果我移除背景图像显示正确,如果我这样做,我的9面片拉伸得非常奇怪。我有一个固定的标志在9补丁的中间,如果我把它设置成SRC,它会被拉伸,但是如果我把它设置成背景,它看起来不错:SIF我设置我的9补丁作为一个SRC,它被扭曲(模糊和固定内容缩放)。试一下所有的磅秤型号,但都不适合。你确定你的9号补丁打得对吗?也许不用打9号补丁就试试吧。。它没有任何阴影或透明像素
imageView.setImageBitmap(image);
<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >

        <View 
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:background="@drawable/image_placeholder"
            />

        <ImageView
            android:id="@+id/article_image"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:adjustViewBounds="true" 
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            />
...