Get the resource ID through reflection in Android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.