我知道存在从另一个包中安装资源 xml 文件的能力,代码如下:
String resourceName = getResources().getResourceEntryName(layoutResID);
String resourceTypeName = getResources().getResourceTypeName(layoutResID);
Resources skinResources = getResourcesForPackage("com.my.skin");
int skinResourceId = skinResources.getIdentifier(resourceName, resourceTypeName, "com.my.skin");
mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLayoutInflater.inflate(skinResources.getXml(skinResourceId), null);
super.setContentView(mLayoutInflater.inflate(skinResources.getXml(skinResourceId), null));
但我想知道如何在 xml 和可绘制资源中制作相同的内容:
例如:我有两个应用:
- 应用 main 包“com.my.main”
- 和应用皮肤包“com.my.skin”
在安卓中docs解释:
May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".
我试图指定这样的东西:
android:background="@com.my.skin:drawable:background_image"
或者这个:
android:background="?com.my.skin:drawable:background_image"
但它不起作用,我的资源文件没有编译。
谁知道如何使用显式规范加载资源。
最佳答案
将您的项目设为库项目,您可以在另一个android项目中引用它。
图书馆项目
这些项目包含可共享的 Android 源代码 和您可以在 Android 项目中引用的资源。当您有想要重用的公共(public)代码时,这很有用。库项目无法安装到设备上,但是,它们会在构建时被拉入 .apk 文件。
有关详细信息,请查看主题图书馆项目下的链接。
http://developer.android.com/tools/projects/index.html
您的库项目将勾选一个复选框,如项目中所示。
您可以将库项目添加到您的 android 项目中。右键单击您的项目。转到属性。在左侧面板中选择 Android。
关于android - 如何从xml中的另一个包加载资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15863776/