开发者

How set an image in Android ImageView

I开发者_如何转开发 need to show a image, when i press a button on my application.

I'm trying with:

ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageDrawable(R.drawable.loading);

but it shows an error.

Anyone can help me? Thanks.


import package_name.R.drawable;

ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageResource(drawable.loading);

OR

you can set the image from the start, set it to invisible and when you want to show it just change the visibility property.


for future visitors:

sometime setImageDrawable(Drawable) method show an error like:

The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (int)

can try following codes:

ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(R.drawable.your_image);
imageView.setImageDrawable(image);

--------- OR can try this ------------------

imageView.setImageDrawable(ContextCompat.getDrawable(YourActivity.this, R.drawable.your_image));

hope it work for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜