ImageView.setBackgroundResource 和 ImageView.setImageResource 有什么区别? [英] What is the difference between ImageView.setBackgroundResource and ImageView.setImageResource?

查看:25
本文介绍了ImageView.setBackgroundResource 和 ImageView.setImageResource 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置图像时看到了这些不同的方法,但我不明白其中的区别.

I have seen these different approaches in setting images but I don't get the difference.

为什么有两种方法?

推荐答案

setBackgroundResource用于设置ImageView的背景.
setImageResource 用于设置ImageView 的src 图片.给定:

setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView. Given:

ImageView iv = new ImageView(this);

那么:

iv.setBackgroundResource(R.drawable.imagedata);

将适合整个背景的图像.这意味着即使图像尺寸太小,它也会拉伸图像以完全填充该背景.

Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.

imageView.setImageResource(R.drawable.imagedata);

将仅占用 ImageView 中图像的大小.为此,您还想设置

Will occupy only the size of the image in ImageView. For that you want to also set

android:layout_width="wrap_content"
android:layout_height="wrap_content"

为您的 ImageView.如果图像的尺寸小于 ImageView 剩余的边框将留空并显示背景.

for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.

这篇关于ImageView.setBackgroundResource 和 ImageView.setImageResource 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆