[Android] 圖片要怎麼填滿layout/填滿螢幕?(How to make ImageView fill the layout/screen?)

若你的parent layout就是整個螢幕的話
在layout屬性裡面加入以下兩行

android:scaleType="centerCrop"
android:adjustViewBounds="true"

就會填滿螢幕了


所以,完整的ImageView部分就會是這樣:

    <ImageView
        android:id="@+id/imageViewDemo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/demo.png"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"/>

除了centerCrop,還有各種的屬性可以填
center、centerInside、fitCenter、fitEnd、fitStart、fitXY、matrix

有興趣可以參考這篇討論:https://stackoverflow.com/questions/6669764/scale-an-image-up-to-fill-entire-imageview-in-android

還有官方說明:https://developer.android.com/reference/android/widget/ImageView#attr_android:scaleType

作者

RongSon

Graduate Student of CCU COMM Game Development, Network Communication, macOS/Ubuntu/Android, Arduino/Raspberry Pi/Intel Edison, Java/Python/C/C++

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *