Android obtains the resource Id through reflection, and android obtains the resource id

Source: Internet
Author: User

Android obtains the resource Id through reflection, and android obtains the resource id

When you compress a self-written tool into A. jar package, you sometimes need to reference the resources in res. At this time, you cannot package the resources together and can only obtain resources dynamically through the reflection mechanism.

Especially when you define a tool to compress it into a. jar package, pay special attention to resource acquisition.

 

1. encapsulated into a tool class

package com.cp.utils;import android.content.Context;public class CPResourceUtil {      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());     }     public static int getArrayId(Context paramContext, String paramString) {         return paramContext.getResources().getIdentifier(paramString,                 "array", paramContext.getPackageName());     } }

  

2. Resource Acquisition

<String name = "version_update_schedule"> % 1 $ s downloaded. Please wait... </String>

  

3. java

String text = mContext. getResources (). getString (CPResourceUtil. getStringId (mContext, "version_update_schedule"); TextView. setText (String. format (text, p + "%"); version_update_schedule is the name in the string File

  

Related Article

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.