[英]How set an image in Android ImageView
當我按應用程序上的按鈕時,我需要顯示圖像。
我正在嘗試:
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);
要么
您可以從頭開始設置圖像,將其設置為不可見,而要顯示它,只需更改可見性屬性即可。
對於未來的訪客:
有時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));
希望它為您工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.