如何引用一个字符串在code一个Android库的strings.xml? [英] How to reference a string in strings.xml of an Android library in code?

查看:454
本文介绍了如何引用一个字符串在code一个Android库的strings.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要引用字符串富在strings.xml中(在res \\值)的应用程序的项目,我们可以简单地使用

To reference string foo in strings.xml (in res\values) of an app project, one can simply use

getString(R.string.foo)

的getString是上下文的方法。

getString is a method of Context.

假设一个Android库在其strings.xml中的字符串foo中。怎么能在图书馆的方法来使用?

Suppose an Android library has a string foo in its strings.xml. How can it be used in a method of the library?

编辑:
已经提出向上下文的引用传递到库方法如此的getString()都可以使用。因为它是一个应用程序的项目的情况下,有可被示出为下面的潜在冲突

Edited: It has been suggested to pass a reference of Context to the library method so getString() can be used. Since it is a context of an app's project, there is potential conflict that can be illustrated as following:

假设:
图书馆与价值=foo库的字符串foo中。
一个应用程序的项目有值的字符串富=应用程序富

Suppose: The library has a string foo with value = "library foo". An app project has a string foo with value = "app foo"

以下code

Log.d("Debug", "App foo ID: " + R.string.foo);  
Log.d("Debug", "App: foo value: " + getString(R.string.foo));

生成:

03-22 05:53:55.590: D/Debug(16719): App foo ID: 2131230723
03-22 05:53:55.590: D/Debug(16719): App foo value: app foo

在库方法,下面code

In a library method, the following code

Log.d("Debug", "Library foo ID: " + R.string.foo); 
Log.d("Debug", "Library foo value: " + context.getString(com.my.library.R.string.foo));

生成:

03-22 05:55:03.680: D/Debug(16719): Library foo ID: 2131230723
03-22 05:55:03.680: D/Debug(16719): Library foo value: app foo

以上显示ID冲突,因此错误的字符串值。

The above shows the ID conflict hence erroneous string value.

推荐答案

正如你指出来引用您的库中定义一个字符串资源,你可以使用getString()方法,你只需要一个上下文,但在你的榜样的冲突通过琴弦的相同名称生成

As you indicated to reference a string resource defined in your library you can use getString() method, you only need a Context, but in your example the conflict is generated by the same name of the strings.

当您在库中定义的资源有冲突,如果你使用的应用程序模块中的资源同名。为了避免您在使用存储库中的资源名称的通用preFIX使用独特的名称,例如冲突,这是不够的不同的包名。

When you define a resource in your library you have a conflict if you use the same name of a resource in the application module. To avoid conflicts you have to use unique names, for example using a common prefix for resource names in the library, it is not sufficient the different package name.

要解决,你可以使用下面的名称:

To resolve you can use the following names:


  • mylib_foo 在库

  • 在应用程序

  • mylib_foo in your library
  • foo in your application

从Android文档资料来源: http://developer.android.com/tool​​s /projects/index.html#considerations

Source from android documentation: http://developer.android.com/tools/projects/index.html#considerations

当你开发你的库模块和相关的应用程序,保持
  记住下面列出的几点:

As you develop your library modules and dependent applications, keep the points listed below in mind:


      
  • 资源冲突

  • Resource conflicts

由于工具合并一个库模块的资源与那些
  依赖应用模块,一个给定的资源ID可能是
  在这两个模块中定义。在这种情况下,工具选择资源
  从应用程序,或具有最高优先级的库,并且
  丢弃等资源。当你开发的应用程序,请注意
  这一共同的资源ID有可能在一个以上的被定义
  项目和将被合并,与该资源从应用程序或
  优先级最高的库回吐precedence。

Since the tools merge the resources of a library module with those of a dependent application module, a given resource ID might be defined in both modules. 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.

使用prefixes,以避免资源冲突

Use prefixes to avoid resource conflicts

要避免公共资源ID的资源冲突,可以考虑使用
  一个preFIX或其他一致的命名方案,该方案是独一无二的
  模块(或在所有项目中的模块是唯一的)。

To avoid resource conflicts for common resource IDs, consider using a prefix or other consistent naming scheme that is unique to the module (or is unique across all project modules).

这篇关于如何引用一个字符串在code一个Android库的strings.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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