ImageView

本文介绍日常开发过程中使用ImageView需要了解的知识和注意的地方,例如:ImageView引用图片后上下方区域空白的问题。

ImageView引用图片后上下方区域空白

注:图片本身上下方无空白区域!!!

当我们将一张图片引用到我们指定的ImageView容器中,可能由于图片尺寸、比例等原因,无法铺满整个容器(表述不好),导致白边的出现,而且怎么去也去不掉。如下布局代码:

<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="测试空白区域文本"/>

</LinearLayout>

如上述布局代码,本身没有任何问题,但是因为图片的尺寸,比例等原因,就可能出现图片和文本之间存在空白区域的问题。

那么怎么解决呢?

其实很简单,我们只需要为ImageView添加以下属性就可以了:

android:adjustViewBounds="true"

adjustViewBounds

我想,你一定会问,这句话代表着什么意思?为什么这样就可以了呢?这里我就给大家简要解释一下:

Google官方对于adjustViewBounds这个属性是这样介绍的:

谷歌原文:Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

中文翻译:如果你想要ImageView适应边界,同时保持图片宽高比,可以将它设置为true。

我们也可以这样理解:将 android:adjustViewBounds 设置为true,即可去除因图片宽高比等原因导致ImageView直接引用图片时上下方存在空白区域的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值