How to get Resource(int) from String - Android [duplicate]

喜夏-厌秋 提交于 2019-12-17 19:27:59

问题


Possible Duplicate:
Android, getting resource ID from string?

String res = "R.drawable.image1";

I need to get it's INT id. Please help me!


回答1:


Use this to get resource id:

int picId = getResources().getIdentifier(picName, "drawable", getApplicationContext().getPackageName());



回答2:


If you want to get the integer then simply remove the quotes.

int id = R.drawable.image1; // instead of "R.drawable.image1"

That will give you the number.




回答3:


View view = findViewById(R.drawable.image1);
int id = view.getId();


来源:https://stackoverflow.com/questions/10170904/how-to-get-resourceint-from-string-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!