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

2019-01-18 13:12发布

Possible Duplicate:
Android, getting resource ID from string?

String res = "R.drawable.image1";

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

3条回答
太酷不给撩
2楼-- · 2019-01-18 13:50

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楼-- · 2019-01-18 13:54

Use this to get resource id:

int picId = getResources().getIdentifier(picName, "drawable", getApplicationContext().getPackageName());
查看更多
混吃等死
4楼-- · 2019-01-18 14:01
View view = findViewById(R.drawable.image1);
int id = view.getId();
查看更多
登录 后发表回答