android jar包方式获取资源

来源:互联网 发布:ov9650摄像头数据手册 编辑:程序博客网 时间:2024/04/29 20:39

本文从其他博客摘录,我们常碰到将程式打包成jar方式发给别人调用,但在获取资源时用普的R.id方式会导致报异常(找不到)

public class Helper
{
    public static int getLayoutId(Context context ,String name)
    {
        return context.getResources().getIdentifier(name, "layout", context.getPackageName());
    }
    
    public static int getResId(Context context ,String name)
    {
        return context.getResources().getIdentifier(name,"id",context.getPackageName());
        
    }
    
    public static int getResDraw(Context context ,String name)
    {
        return context.getResources().getIdentifier(name,"drawable",context.getPackageName());
        
    }
    
    public static int getResStyle(Context context ,String name)
    {
        return context.getResources().getIdentifier(name,"style",context.getPackageName());
        
    }
    
    public static int getResStr(Context context ,String name)
    {
        return context.getResources().getIdentifier(name,"string",context.getPackageName());        
    }
    
    public static int getResXml(Context context ,String name)
    {
        return context.getResources().getIdentifier(name,"xml",context.getPackageName());        
    }
}

0 0
原创粉丝点击