2010-09-23 51 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"/>