从库项目中访问应用程序资源 [英] Accessing application resources from the library project

查看:130
本文介绍了从库项目中访问应用程序资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

menu.xml文件文件是应用程序项目中。
所有的Java code是库项目中,包括了菜单句柄code onOptionsItemSelected()

有没有办法从库项目中访问应用程序的资源?我喜欢写这样的事情,这是目前不可能的,因为菜单项没有从库中可见的:

 如果(item.getItemId()== R.id.settings){
...
}
 

解决方案

是的,你可以的,如果你知道你的库包的名称。
<一href="http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29">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");

这篇关于从库项目中访问应用程序资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆