Android access to resources outside of activity

后端 未结 2 2000
萌比男神i
萌比男神i 2021-01-13 21:09

I would like to know - are there ways to access android resources and/or assets files (or files from any other folder) outside of an Activity (without passing context

2条回答
  •  独厮守ぢ
    2021-01-13 21:31

    Android framework when compile your app create static class call:

    your.namespace.project.R
    

    you can statically call this class like:

    your.namespace.project.R.string.my_prop
    
    this.context.findViewById(your.namespace.project.R.id.my_id_prop);
    

    Maybe you can access dynamic resources this way:

    Resources res = this.context.getResources();
    int id = res.getIdentifier("bar"+Integer.toString(i+1), "id", this.context.getPackageName());
    

提交回复
热议问题