我已经看到了这些不同的方法设置图片,但我不明白的区别。
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.
相关推荐
最新文章