当我按下应用程序上的按钮时,我需要显示图像。
我正在尝试:
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageDrawable(R.drawable.loading);
但它显示一个错误。
任何人都可以帮助我吗?谢谢。
当我按下应用程序上的按钮时,我需要显示图像。
我正在尝试:
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageDrawable(R.drawable.loading);
但它显示一个错误。
任何人都可以帮助我吗?谢谢。
import package_name.R.drawable;
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageResource(drawable.loading);
或者
您可以从一开始就设置图像,将其设置为不可见,当您想要显示它时,只需更改可见性属性。
对于未来的访客:
sometimesetImageDrawable(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));
希望它对你有用。