Android ImageView 使用

189 阅读1分钟

一、ImageViewsrc 属性与 background 的区别

  • background 通常指的都是背景,而 src 指的是内容。

  • 当使用 src 填入图片时,是按照图片大小百接填充,并不会进行拉伸,而使用 background 填入图片,则是会根据 ImageView 给定的宽度来进行拉伸。

    <!-- ImageView 根据内容适应宽度 -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>
    <!-- ImageView 限制宽度 -->
    <ImageView
        android:layout_width="160dp"
        android:layout_height="80dp"
        android:background="@mipmap/ic_launcher"/>
    <ImageView
        android:layout_width="160dp"
        android:layout_height="80dp"
        android:src="@mipmap/ic_launcher"/>
    <!-- ImageView 有图片有背景 -->
    <ImageView
        android:layout_width="160dp"
        android:layout_height="80dp"
        android:background="#ffc"
        android:src="@mipmap/ic_launcher"/>
    

    image.png

二、scaleType 属性详细介绍与使用