ImageView.setBackgroundResource和ImageView.setImageResource之间的区别是什么? [英] What is the difference between ImageView.setBackgroundResource and ImageView.setImageResource?

查看:489
本文介绍了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天全站免登陆