3 回答
TA貢獻2016條經驗 獲得超9個贊
請注意,您的方法幾乎總是錯誤的處理方式(最好是將上下文傳遞給使用drawable的對象本身,而不是在Context某個地方保持靜態(tài))。
鑒于此,如果要進行動態(tài)可繪制加載,則可以使用getIdentifier:
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(name, "drawable",
context.getPackageName());
return resources.getDrawable(resourceId);
TA貢獻1805條經驗 獲得超10個贊
你可以做這樣的事情。
public static Drawable getDrawable(String name) {
Context context = YourApplication.getContext();
int resourceId = context.getResources().getIdentifier(name, "drawable", YourApplication.getContext().getPackageName());
return context.getResources().getDrawable(resourceId);
}
為了從任何地方訪問上下文,您可以擴展Application類。
public class YourApplication extends Application {
private static YourApplication instance;
public YourApplication() {
instance = this;
}
public static Context getContext() {
return instance;
}
}
并將其映射到您的Manifest application標簽中
<application
android:name=".YourApplication"
....
TA貢獻1818條經驗 獲得超7個贊
修改圖片內容:
ImageView image = (ImageView)view.findViewById(R.id.imagenElement);
int resourceImage = activity.getResources().getIdentifier(element.getImageName(), "drawable", activity.getPackageName());
image.setImageResource(resourceImage);
- 3 回答
- 0 關注
- 614 瀏覽
添加回答
舉報
