最近打了jar包給另外一個項目用但是裏面的viewcp.findViewById(R.id.aa)類似這種的找不到 一直爲空,後來用動態獲取id解決
封裝了個方法
//獲取資源ID
public static int getResourcesID(Context context,String idname,String localxy){
Resources res=context.getResources();
return res.getIdentifier(idname,localxy,context.getPackageName());
}
參數:
idname代表你的資源的名字
localxy代表你的資源在那個文件夾中例如layout id drawable string .........
在要打包的jar包裏面直接調用就行
例如:
viewcp = LayoutInflater.from(context).inflate(Tools.getResourcesID(context, "activity_showpic", "layout"), null);
ImageView imageView = (ImageView) viewcp.findViewById(Tools.getResourcesID(context, "pic", "id"));
ImageView imageclose = (ImageView) viewcp.findViewById(Tools.getResourcesID(context, "close", "id"));