简体   繁体   English

如何在Android ImageView中设置图像

[英]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: 有时setImageDrawable(Drawable)方法显示如下错误:

The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (int) ImageView类型的setImageDrawable(Drawable)方法不适用于参数(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. 希望它为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM