How to get a resource id with a known resource name?

前端 未结 10 1397
挽巷
挽巷 2020-11-21 23:31

I want to access a resource like a String or a Drawable by its name and not its int id.

Which method would I use for this?

10条回答
  •  轮回少年
    2020-11-22 00:31

    I have found this class very helpful to handle with resources. It has some defined methods to deal with dimens, colors, drawables and strings, like this one:

    public static String getString(Context context, String stringId) {
        int sid = getStringId(context, stringId);
        if (sid > 0) {
            return context.getResources().getString(sid);
        } else {
            return "";
        }
    }
    

提交回复
热议问题