Java uses reflection to get resources

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

123456789101112131415161718192021222324252627282930313233343536373839 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. Access to resources

1 <stringname="version_update_schedule">已下载%1$s请稍等&amp;#8230;</string>
1 String text=mContext.getResources().getString(CPResourceUtil.getStringId(mContext, "version_update_schedule"));

Textview.settext (String.Format (text, p+ "%"));

1 version_update_schedule 是string文件中name

Java uses reflection to get resources

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.