How to get a resource id with a known resource name?

前端 未结 10 1264
挽巷
挽巷 2020-11-21 23:31

I want to access a resource like a String or a Drawable by its name and not its int id.

Which method would I use for this?

10条回答
  •  北海茫月
    2020-11-22 00:31

    In Kotlin following works fine for me:

    val id = resources.getIdentifier("your_resource_name", "drawable", context?.getPackageName())
    

    If resource is place in mipmap folder, you can use parameter "mipmap" instead of "drawable".

提交回复
热议问题