2010-09-23 53 views
33

因此,我从Web服务加载图像,但图像的大小有时比其他图像的大小要小,并且可视化看起来很愚蠢我把它们放在Android的ListView中。我想修复ImageView的大小,使其只显示图像的一部分,如果它大于预设的数量。我尝试了所有我能想到的设置setMaxWidth/setMaxHeight,将缩放类型设置为centerCrop,使用ClipableDrawable封装我的BitmapDrawable,使用Drawable.setBounds()进行设置。我试着用XML和编程方式设置,但都没有成功。我很惊讶设置最大宽度/高度没有做任何事情。以下是我在布局文件中使用ImageView定义的XML:无论位图的大小如何使我的ImageView成为固定大小

<ImageView android:id="@+id/recipeImage" 
      android:maxHeight="64px" 
      android:maxWidth="64px" 
      android:scaleType="centerCrop" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentBottom="true" 
      android:layout_marginRight="6dip"/> 

没有任何工作。 Oooooouweee什么与dat? :-)

回答

41

只需删除maxHeight和maxWidth属性,并适当地在layout_width和layout_height中输入您的单位。但不要使用“px”单元 - 而是使用“dp”,因为这些设备无关。

+1

我可以发誓我试过了。谢谢你的帮助。 – chubbsondubs 2010-09-27 13:24:41

+0

'minHeight'和'minWidth'如何? – 2016-09-13 22:39:24

0

这将使图像适合指定的高度和宽度,与图像大小无关。

<ImageView 
     android:id="@+id/image5" 
     android:layout_width="300dp" 
     android:layout_height="200dp" 
     android:src="@drawable/image_1" 
     android:scaleType="fitXY"/>