问题描述
我的应用程序依赖于库项目。
在 menu.xml文件文件是应用程序项目中。
所有的Java code是库项目中,包括了菜单句柄code onOptionsItemSelected()
有没有办法从库项目中访问应用程序的资源?我喜欢写这样的事情,这是目前不可能的,因为菜单项没有从库中可见的:
如果(item.getItemId()== R.id.settings){
...
}
是的,你可以的,如果你知道你的库包的名称。
Resources.getIdentifier(...)
您可以做的:
getResources()则getIdentifier(RES_NAME,res_type,com.library.package);
例如:
R.id.settings
是:
getResources()则getIdentifier(设置,ID,com.library.package);
My application depends on a library project.
The menu.xml file is within the application project.
All the java code is within the library project, including the menu handler code onOptionsItemSelected().
Is there a way to access the application resources from library project ? I'd like to write something like this, which is currently impossible, since menu items are not visible from the library:
if ( item.getItemId()==R.id.settings ) {
...
}
Yes you can if you know the package name of your library.
Resources.getIdentifier(...)
You can do:
getResources().getIdentifier("res_name", "res_type", "com.library.package");
ex:
R.id.settings
would be:
getResources().getIdentifier("settings", "id", "com.library.package");
这篇关于从库项目中访问应用程序资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!