资源文件帮助类ResourceHelper

public class ResourceHelper {

    private ResourceHelper() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    private static final String RES_ID = "id";
    private static final String RES_STRING = "string";
    private static final String RES_DRABLE = "drable";
    private static final String RES_LAYOUT = "layout";
    private static final String RES_STYLE = "style";
    private static final String RES_COLOR = "color";
    private static final String RES_DIMEN = "dimen";
    private static final String RES_ANIM = "anim";
    private static final String RES_MENU = "menu";


    /**
     * @return  获取上下文对象
     */
    public static Context getContext() {
        return App.getContext();
    }

    /**
     *
     * @return  获取资源Android资源
     */
    public static Resources getResource(){
        return getContext().getResources();
    }

    /**
     *
     * @param resId
     * @return
     */
    public static String getString(int resId){
        return getResource().getString(resId);
    }

    /**
     *
     * @param resId
     * @return
     */
    public static Drawable getDrawable(int resId){
        return getResource().getDrawable(resId);
    }
    /**
     *
     * @param resId
     * @return
     */
    public static int getColor(int resId){
        return getResource().getColor(resId);
    }
    /**
     *
     * @param resId
     * @return
     */
    public static float getDimens(int resId){
        return getResource().getDimension(resId);
    }

    /**
     * 获取资源文件的id
     *
     * @param resName
     * @return
     */
    public static int getId(String resName) {
        return getResId(resName, RES_ID);
    }

    /**
     * 获取资源文件string的id
     *
     * @param resName
     * @return
     */
    public static int getStringId(String resName) {
        return getResId(resName, RES_STRING);
    }

    /**
     * 获取资源文件drable的id
     *
     * @param resName
     * @return
     */
    public static int getDrableId(String resName) {
        return getResId(resName, RES_DRABLE);
    }

    /**
     * 获取资源文件layout的id
     *
     * @param resName
     * @return
     */
    public static int getLayoutId(String resName) {
        return getResId(resName, RES_LAYOUT);
    }

    /**
     * 获取资源文件style的id
     *
     * @param resName
     * @return
     */
    public static int getStyleId(String resName) {
        return getResId(resName, RES_STYLE);
    }

    /**
     * 获取资源文件color的id
     *
     * @param resName
     * @return
     */
    public static int getColorId(String resName) {
        return getResId(resName, RES_COLOR);
    }

    /**
     * 获取资源文件dimen的id
     *
     * @param resName
     * @return
     */
    public static int getDimenId(String resName) {
        return getResId(resName, RES_DIMEN);
    }

    /**
     * 获取资源文件ainm的id
     *
     * @param resName
     * @return
     */
    public static int getAnimId(String resName) {
        return getResId(resName, RES_ANIM);
    }

    /**
     * 获取资源文件menu的id
     *
     * @param resName
     */
    public static int getMenuId(String resName) {
        return getResId(resName, RES_MENU);
    }

    /**
     * 获取资源文件ID
     *
     * @param resName
     * @param defType
     * @return
     */
    public static int getResId(String resName, String defType) {
        return getResource().getIdentifier(resName, defType, getContext().getPackageName());
    }

}

猜你喜欢

转载自blog.csdn.net/nuonuonuonuonuo/article/details/73123360