2011-05-17 138 views
0

当我在应用程序上按下按钮时,需要显示图像。如何在Android ImageView中设置图像

我与努力:

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

,但它显示了一个错误。

任何人都可以帮到我吗?谢谢。

+0

你有错误日志,你可以添加到问题?会使它更容易帮助你。 – Lee 2011-05-17 14:37:56

+0

它给了什么错误? – 2011-05-17 14:38:27

+0

ImageView类型中的setImageDrawable(Drawable)方法不适用于参数(int) – fr4n 2011-05-17 14:39:36

回答

6
import package_name.R.drawable; 

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

OR

,你可以从一开始就设定的图像,将其设置为不可见的,当你想要显示它只是改变了可见性属性。

+0

完美,谢谢;) – fr4n 2011-05-17 14:45:38

+0

很高兴帮助...享受 – 2011-05-17 14:53:02

0

为未来的用户:

有时setImageDrawable(Drawable)方法显示如下错误:

在类型ImageView的方法setImageDrawable(可绘制)不适用于参数(INT)

可以尝试以下代码:

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

---------或者可以试试这个------------------

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

希望它适合你。

相关问题