Sometimes, we want to provide our android project to third-party developers. At this time, the most common practice is to provide a jar package and a bunch of resource files. Third-party developers can copy the resource files to the corresponding directory of the android project, reference the jar package we provide at the same time, you can use the corresponding API we provide.
Please note that the jar package and resource package are provided to third-party developers.CodeYou cannot reference resources in a way similar to R. layout. Main or R. String. Name.
Therefore, we cannot directly use the r file. Instead, we need to dynamically obtain the resource ID through the field name.
The following encapsulates a class and dynamically obtains the ID using the field name.
Package COM. arui. util; import android. content. context; public class resourceutil {public static int getlayoutid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "layout", paramcontext. getpackagename ();} public static int getstringid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "string", paramcontext. getpackagename ();} public static int getdrawableid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "drawable", paramcontext. getpackagename ();} public static int getstyleid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "style", paramcontext. getpackagename ();} public static int GETID (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "ID", paramcontext. getpackagename ();} public static int getcolorid (context paramcontext, string paramstring) {return paramcontext. getresources (). getidentifier (paramstring, "color", paramcontext. getpackagename ());}}
Of course, if you can write all the resource files into the jar package, it is the most convenient for third-party developers. To do this, you must completely process the layout, image resources, and so on in the code, which is relatively troublesome. If there are few elements such as the UI, it is worth trying.
---------------------------------------------------------------------------
GL(Arui319)
Http://blog.csdn.net/arui319
<This article can be reproduced, but please keep the above author information. Thank you.>
---------------------------------------------------------------------------