本文介紹日常開發(fā)過程中使用ImageView需要了解的知識(shí)和注意的地方,例如:ImageView引用圖片后上下方區(qū)域空白的問題。
ImageView引用圖片后上下方區(qū)域空白
注:圖片本身上下方無空白區(qū)域!!!
當(dāng)我們將一張圖片引用到我們指定的ImageView容器中,可能由于圖片尺寸、比例等原因,無法鋪滿整個(gè)容器(表述不好),導(dǎo)致白邊的出現(xiàn),而且怎么去也去不掉。如下布局代碼:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/share_invite"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測(cè)試空白區(qū)域文本"/>
</LinearLayout>
如上述布局代碼,本身沒有任何問題,但是因?yàn)閳D片的尺寸,比例等原因,就可能出現(xiàn)圖片和文本之間存在空白區(qū)域的問題。
那么怎么解決呢?
其實(shí)很簡單,我們只需要為ImageView添加以下屬性就可以了:
android:adjustViewBounds="true"
adjustViewBounds
我想,你一定會(huì)問,這句話代表著什么意思?為什么這樣就可以了呢?這里我就給大家簡要解釋一下:
Google官方對(duì)于adjustViewBounds這個(gè)屬性是這樣介紹的:
谷歌原文:Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
中文翻譯:如果你想要ImageView適應(yīng)邊界,同時(shí)保持圖片寬高比,可以將它設(shè)置為true。
我們也可以這樣理解:將 android:adjustViewBounds 設(shè)置為true,即可去除因圖片寬高比等原因?qū)е翴mageView直接引用圖片時(shí)上下方存在空白區(qū)域的問題。