从库项目中访问应用程序资源应用程序、项目、资源

2023-09-06 02:30:48 作者:回忆后的°不怀念

我的应用程序依赖于库项目。

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

Collabtive下载 Collabtive个人项目管理软件v3.2 官方版 腾牛下载

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