Android ImageView比例类型忽略填充

Android ImageView比例类型忽略填充,android,gridview,imageview,border,Android,Gridview,Imageview,Border,我有一个带有白色背景和1dp填充的Imageview集,这创建了一个类似边框的效果,这是想要的结果 现在,如果我将scaleType设置为centerCrop,它将忽略顶部和底部的填充。 因此,我的边界仍然在左侧和右侧,但不在顶部和底部 有人想阻止这种事情发生吗? 或者另一种在图像周围创建边框的快速方法。我将其用于自定义gridview <ImageView android:layout_width="fill_parent" andr

我有一个带有白色背景和1dp填充的Imageview集,这创建了一个类似边框的效果,这是想要的结果

现在,如果我将scaleType设置为centerCrop,它将忽略顶部和底部的填充。
因此,我的边界仍然在左侧和右侧,但不在顶部和底部

有人想阻止这种事情发生吗? 或者另一种在图像周围创建边框的快速方法。我将其用于自定义gridview

   <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:contentDescription="@string/test"
            android:padding="1dp"
            android:src="@drawable/some_photo"
            android:scaleType="centerCrop" />

您可以通过设置
android:background=“@drawable/edit_border”

和您的编辑边框:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke
        android:width="2dp"
        android:color="#EBDDE2" />

    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="4dp" />

    <gradient
        android:centerColor="@color/white" 
        android:endColor="@color/white"
        android:startColor="@color/white" />

    <corners android:radius="8dp" />

</shape>

我刚刚遇到了同样的问题,因为我的解决方案是添加android:layout\u marginRight=“5dip”



您只需要添加

android:cropToPadding=“true”


然后imageView将尊重您选择的填充。

@Manolo Garcia的答案是正确的。 这有助于我在动态图像视图中使用边框、缩放类型、中心裁剪和填充

final ImageView imageView = new ImageView(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200,200);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
imageView.setAdjustViewBounds(true);
imageView.setCropToPadding(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setBackgroundResource(R.drawable.img_blue_border);
imageView.setPadding(5, 5, 5, 5);

android:scaleType=“fitxy”勾选不会拉伸图像的选项,我正在尝试防止这种情况。另外,设置android:adjustViewBounds=“true”后,它仍然会拉伸图像,而不是使用android:src使用android:Background确实获得了边框,但我的横向图像视图中的肖像图片仍然没有显示顶部和底部边框谢谢,它起到了作用,我以为我不能在gridView中使用额外的线性布局,但事实证明我可以。在所有情况下都不可行。Manolo Garcia有正确的答案Manolo Garcia的答案是正确的,这个保证金权利没有意义。这不是正确的答案,Manolo Garcia的解决方案是正确的。欢迎并感谢您提供替代答案,Manolo。无法测试它,但对我来说,它的价值很高。注意:该属性从api级别1开始就存在,但该方法从JellyBean开始才可用。太棒了,找不到任何解决方案。谢谢!你可能认为他们会在Android 4.4.2上的ImageView.ScaleType的文档中提到这一点:cropToPadding=“true”如果在设置填充后设置背景资源,则该选项不起作用。因此,您必须首先设置背景资源,然后设置填充。见Matt McMinn的答案:
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:contentDescription="@string/test"
    android:padding="1dp"
    android:src="@drawable/some_photo"
    android:cropToPadding="true"
    android:scaleType="centerCrop" />
final ImageView imageView = new ImageView(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200,200);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
imageView.setAdjustViewBounds(true);
imageView.setCropToPadding(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setBackgroundResource(R.drawable.img_blue_border);
imageView.setPadding(5, 5, 5, 5);