java - 在库项目中使用库项目的正确 values/strings.xml

标签 java android xml android-resources library-project

我正在开发一个 Android 应用程序,它使用一个库项目,该项目包含工作区中我的多个应用程序正在使用的通用方法。

在库项目中,我有一个类,其中包含一个显示警报对话框和一些 toast 消息的方法。

我正在尝试做的是使用库的“res/values/strings.xml”值来显示那些警报对话框和 toast 消息。 关键是我当然希望根据当前用户的语言采用相关的字符串值。 这样,每当我从其他应用程序使用库中的那些方法时,我就不必在应用程序的“values/strings.xml”中为每个应用程序重新翻译字符串。

提前致谢!

最佳答案

这就是使用库的美妙之处:应用程序中定义的任何字符串都会覆盖库中的字符串。如果我正确理解了您的问题,那么您无需执行任何操作:只需确保字符串名称保持不变即可。

请参阅 documentation on libraries 中的这句话:

Since the tools merge the resources of a library project with those of a dependent application project, a given resource ID might be defined in both projects. In this case, the tools select the resource from the application, or the library with highest priority, and discard the other resource. As you develop your applications, be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.

关于java - 在库项目中使用库项目的正确 values/strings.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22181755/

相关文章:

php - 我尝试使用 Php、MySQL 在 android 中注册表单并使用 JSON 解析进行响应,但详细信息未插入表中

java - 无法从 Java 进程使用 taskkill.exe

java - Android Studio - 将字节码转换为 dex 时出错,原因 : Dex cannot parse version 52 byte code

java - Android 5.0 及更高版本上未显示 Firebase 通知图标

java - 以编程方式卸载应用程序 : crash rotating the device

xml - vbscript 创建转换带有特殊字符的 xml

java - Spring用单线程池并发处理多个队列

java - 为什么我不能注入(inject)一个 bean 的多个实例?

java - 测量选择排序 for 循环的持续时间

java - 如何向 XML DOM 对象添加命名空间前缀?