当我按下应用程序上的按钮时,我需要显示图像。
我正在尝试:
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageDrawable(R.drawable.loading);
但它显示错误。
任何人都可以帮助我吗?感谢。
答案 0 :(得分:6)
import package_name.R.drawable;
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageResource(drawable.loading);
OR
您可以从头开始设置图像,将其设置为不可见,当您想要显示图像时,只需更改可见性属性。
答案 1 :(得分:0)
面向未来的访问者:
某个时候setImageDrawable(Drawable)
方法显示如下错误:
ImageView类型中的方法setImageDrawable(Drawable)不适用于参数(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));
希望它适合你。