![](/img/trans.png)
[英]How to load image from writeable folder and assign to ImageView in Android?
[英]How to assign Image Resource to ImageView with String in Android
我正在嘗試創建一個例程,該例程將卡片圖像分配給ImageView而不使用諸如R.drawable.card_10clover之類的東西。 我更喜歡使用字符串,但是這樣做的方式行不通。
String imgRes = "R.drawable.card_";
int num = 10;
imgRes += "10";
String suit = "clover";
imgRes += "suit;
ImageView card = (ImageView) findViewById(R.id.imgcard);
card.setImageResource(imgRes);
不幸的是setImageResource只接受一個int值。 請幫忙。
謝謝傑克·布魯
使用getIdentifier(字符串名稱,字符串defType,字符串defPackage); 您的imgRes不需要前綴R.drawable
String imgRes = "card_";
int num = 10;
imgRes += "10";
String suit = "clover";
imgRes += "suit;
ImageView card = (ImageView) findViewById(R.id.imgcard);
int resourceId = getResource().getIdentifier (imgRes, "drawable", "com....");
card.setImageResource(resourceId);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.