我有以下GridView定義w/stretchMode設置爲「columnWidth」。Android GridView拉伸模式與異步ImageView加載
<GridView android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="160dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:listSelector="@drawable/list_selector"
/>
GridView中的每個項目結構如下。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView android:id="@+id/videoGridItemImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
/>
<TextView android:id="@+id/videoGridItemDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#88000000"
android:textColor="#F1F1F1"
android:textAppearance="@android:style/TextAppearance.Small"
android:padding="2dp"
/>
</FrameLayout>
<TextView android:id="@+id/videoGridItemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textAppearance="@android:style/TextAppearance.Medium"
android:textColor="#F1F1F1"
android:maxLines="1"
android:background="#88000000"
android:paddingLeft="2dp"
android:paddingRight="2dp"
/>
<TextView android:id="@+id/videoGridItemSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textStyle="italic"
android:textColor="#666"
android:textAppearance="@android:style/TextAppearance.Small"
android:maxLines="1"
android:background="#88000000"
android:paddingLeft="2dp"
android:paddingRight="2dp"
/>
</LinearLayout>
在GridView的適配器中,我通過將ImageView的URL和引用傳遞給專用類來異步加載並顯示圖像。該類將圖像下載到位圖中,然後使用setImageBitmap()更新ImageView。此工作流程按預期運行;但是,我的圖像並未填充ImageView的整個寬度。在左側/右側有少量的paddding。我意識到我可以使用scaleType =「fitXY」,但這並不理想,因爲它會歪斜圖像。使用stretchMode =「columnWidth」將ImageView的src屬性設置爲本地圖像可以很好地縮放。我的猜測是,ImageView縮放到原始columnWidth而不是拉伸/動態寬度。有沒有人有任何見解,爲什麼這是發生和/或如何解決?在此先感謝您的幫助。
1。將ImageView的layout_width設置爲wrap_content只需將所有填充放在右側 - 左側一半,右側一半。 ImageView仍然不會根據需要填充整列的寬度。 – ajporterfield
2.將ImageView的scaleType設置爲centerCrop可以修復由fitXY引起的歪斜問題;然而,它也會造成不理想的形象。 – ajporterfield
我很確定問題出在stretchMode =「columnWidth」。如果我爲stetchMode使用任何其他值(包括「none」),圖像將按預期顯示 - 不裁剪/傾斜並填充整個列寬。 – ajporterfield