2011-07-28 110 views
2

我有一個ListFragment與自定義ListAdapter懶惰加載圖像並顯示一個視圖與左側的圖像和一些文本在右側。自定義列表項我使用看起來像這樣:ListFragment圖片更改大小

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/image" 
     android:maxWidth="80dp" 
     android:adjustViewBounds="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:padding="10dp" 
     android:textSize="16sp" 
     android:gravity="left|center_vertical" /> 

</LinearLayout> 

這是所有工作正常,但是當我點擊一個列表項,然後單擊後退,列表顯示與他們的全尺寸圖像次數最多,不在指定的80dp寬度。如果在按下之前等待一段時間,列表有時會正確加載。

有誰知道我該如何解決這個問題?我希望圖像始終以80dp寬度顯示。謝謝。

回答

1

這是我如何使用不同寬度和高度的圖像。強制每個圖像85x85,裁剪。

<ImageView 
     android:src="@drawable/some_img" 
     android:layout_width="85dip" 
     android:layout_height="85dip" 
     android:scaleType="centerCrop" 
    /> 

您可能會考慮使用centerCrop或centerInside。

如果android:adjustViewBounds="true"導致您看到的問題,我不會感到驚訝。

另一個罪魁禍首可能是你在TextView設置android:layout_height="fill_parent"這應該是wrap_content

+0

您好,感謝您的回答。我不想裁剪圖像,我想調整它。 'adjustViewBounds'是爲了確保'ImageView'只佔用縮放圖像的空間,否則它將保留原始圖像的比例。 'TextView'的高度設置爲'fill_parent',所以我可以垂直居中 - 雖然這個shuld也可以用'wrap_text'和'layout_gravity' - 我現在嘗試 – Pikaling

+0

不要從'fill_parent'改變爲'wrap_content '沒有幫助,圖像仍然保持縮小到全尺寸。 – Pikaling

+0

我嘗試刪除'android:adjustViewBounds =「true」',但這並沒有什麼區別 – Pikaling