When you write your own tools into a. jar package, you sometimes need to refer to the resources in res, and you cannot package the resources together, but you can obtain resources dynamically through the reflection mechanism.
Especially when you define a tool to make it into a. jar package, pay particular attention to resource acquisition
1, packaged into a tool class
Package Com.cp.utils;import Android.content.context;public class Cpresourceutil {public static int Getlayoutid (Conte XT 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.getresource S (). Getidentifier (paramstring, "drawable", Paramcontext.getpackagename ()); public static int Getstyleid (Context paramcontext, String paramstring) {return PARAMCONTEXT.GETRESOURC Es (). 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.GETRESOURC Es (). Getidentifier (paramstring, "Color", paramcontext.getpackagename ()); public static int Getarrayid (Context paramcontext, String paramstring) {return paramcontext.getresources (). Getidentifier (paramstring, "array", Paramcontext.getpackagename ()); } }
2. Access to resources
<string name= "Version_update_schedule" > downloaded%1 $ s Please wait a moment ...</string>
3.java
String text=mcontext.getresources (). GetString (Cpresourceutil.getstringid (Mcontext, "Version_update_schedule")); Textview.settext (String.Format (text, p+ "%")); Version_update_schedule is a String file in the name
Get the resource ID through reflection in Android