package com.example.qyh.joe.theme; import android.content.res.ColorStateList; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.text.TextUtils; public class ResourceManager { private static final String DEFTYPE_DRAWABLE = "drawable"; private static final String DEFTYPE_COLOR = "color"; private Resources mResources; private String mPluginPackageName; private String mSuffix; public ResourceManager(Resources res, String pluginPackageName, String suffix) { mResources = res; mPluginPackageName = pluginPackageName; if (suffix == null) { suffix = ""; } mSuffix = suffix; } public Drawable getDrawableByName(String name) { try { name = appendSuffix(name); return mResources.getDrawable(mResources.getIdentifier(name, DEFTYPE_DRAWABLE, mPluginPackageName)); } catch (Resources.NotFoundException e) { try { return mResources.getDrawable(mResources.getIdentifier(name, DEFTYPE_COLOR, mPluginPackageName)); } catch (Resources.NotFoundException e2) { e.printStackTrace(); return null; } } } public int getColor(String name) { try { name = appendSuffix(name); return mResources.getColor(mResources.getIdentifier(name, DEFTYPE_COLOR, mPluginPackageName)); } catch (Resources.NotFoundException e) { e.printStackTrace(); return -1; } } public ColorStateList getColorStateList(String name) { try { name = appendSuffix(name); return mResources.getColorStateList(mResources.getIdentifier(name, DEFTYPE_COLOR, mPluginPackageName)); } catch (Resources.NotFoundException e) { e.printStackTrace(); return null; } } private String appendSuffix(String name) { if (!TextUtils.isEmpty(mSuffix)) return name += "_" + mSuffix; return name ; } }