Android一个程序使用另一个程序的资源

来源:互联网 发布:黑马程序员讲师 编辑:程序博客网 时间:2024/04/30 03:37

应用场景:程序A的一个ImageView使用程序B的资源图片。

前提:知道程序B的包名。

关键代码如下:

//Context.CONTEXT_INCLUDE_CODE 是可以调用其他程序的代码                Context.CONTEXT_IGNORE_SECURITY 是资源   //两个一起写都可以

 Context OtherContext = getContext().createPackageContext("程序B的包名",Context.CONTEXT_INCLUDE_CODE |  Context.CONTEXT_IGNORE_SECURITY);

 Resources OtherResources =OtherContext.getResources();

 //ic_app_wu_90  图片的名字   drawable  资源是drawable类型  
 int resID = OtherResources.getIdentifier("ic_app_wu_90", "drawable","程序B的包名");
 mWeatherIcon.setImageDrawable(OtherResources.getDrawable(resID));