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

前端 未结 10 1414
挽巷
挽巷 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条回答
  •  Happy的楠姐
    2020-11-22 00:12

    // image from res/drawable
        int resID = getResources().getIdentifier("my_image", 
                "drawable", getPackageName());
    // view
        int resID = getResources().getIdentifier("my_resource", 
                "id", getPackageName());
    
    // string
        int resID = getResources().getIdentifier("my_string", 
                "string", getPackageName());
    

提交回复
热议问题