問題
ImageView.src設置圖片資源,圖片不拉伸了,卻有空隙部分:
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="205dp" android:scaleType="centerInside" android:background="@drawable/feature_guide_1" > </ImageView> </LinearLayout>

解決
如下方式設置 就沒有空隙了
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:adjustViewBounds="true" android:scaleType="fitXY" android:src="@drawable/feature_guide_0" > </ImageView> </LinearLayout>
以下為參考內容:
最近碰到一個需求,要求是在不知道圖片寬度和高度的情況下,讓圖片在指定寬度內充滿,同時高度自適應,在網絡上查找了一下,也有很多解決方法,后來針對自己的應用,選擇了一個修改較小的方案,最后證明效果還是蠻不錯的,記錄在這里,希望能幫助到有同樣需求的人。
好了,言歸正傳
首先,需要給你的ImageView布局加上Android:adjustViewBounds="true"
<ImageView android:id="@+id/test_image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:scaleType="fitXY"android:adjustViewBounds="true"android:layout_gravity="center"android:contentDescription="@string/app_name"android:src="@drawable/ic_launcher" />
然后,在代碼里設置ImageView.最大寬度和最大高度,因為adjustViewBounds屬性只有在設置了最大高度和最大寬度后才會起作用
int screenWidth = getScreenWidth(this);ViewGroup.LayoutParams lp = testImage.getLayoutParams();lp.width = screenWidth;lp.height = LayoutParams.WRAP_CONTENT;testImage.setLayoutParams(lp);testImage.setMaxWidth(screenWidth);testImage.setMaxHeight(screenWidth * 5);
這里其實可以根據需求而定,我這里測試為最大寬度的5倍
ok,接下來,再按照常規方法加載圖片就會得倒預期的效果了,需要的同學可以試試,good luck.
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。
新聞熱點
疑難解答