How to load resource from another package in xml?

不羁的心 提交于 2019-12-05 03:16:37

问题


I know that exists ability to install resource xml file from another package in code like this:

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));

But i would like to know how to make the same but in xml and for drawable resources:

For example: I have two applications:

  • application main with package "com.my.main"
  • and application skin with package "com.my.skin"

In android docs explained:

May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

I have tried to specify something like this:

android:background="@com.my.skin:drawable:background_image"

or this:

android:background="?com.my.skin:drawable:background_image"

but it not work, my resource file not compiled.

Who know how to load resource using explicit specification.


回答1:


Make your project a library project and you can refer it in another android project.

Library Projects

These projects contain shareable Android source code and resources that you can reference in Android projects. This is useful when you have common code that you want to reuse. Library projects cannot be installed onto a device, however, they are pulled into the .apk file at build time.

For more information have a look at the link under the topic Library Projects.

http://developer.android.com/tools/projects/index.html

Your library project will have a check box ticked as shown in the project.

You can add library project to your android project. Right click on your project. Goto properties. Choose Android on the left panel.



来源:https://stackoverflow.com/questions/15863776/how-to-load-resource-from-another-package-in-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!