如何在 Android 中使用 XML 从库中的另一个包中引用字符串?

How to reference a string from another package in a library using XML in Android?(如何在 Android 中使用 XML 从库中的另一个包中引用字符串?)
本文介绍了如何在 Android 中使用 XML 从库中的另一个包中引用字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

Android documentation 告诉我,我可以访问使用包名"从另一个包中获取字符串,无论这意味着什么:

The Android documentation tells me that I can access a string from another package by using the "package name", whatever that means:

@[<package_name>:]<resource_type>/<resource_name>

所以在我的清单中,我想访问一个字符串,该字符串放在一个单独的库项目中,在 com.globalmentor.android 包中---这就是我的 R 类毕竟是:

So in my manifest I want to access a string that I've placed in a separate library project, in the com.globalmentor.android package---that's where my R class is, after all:

    <activity
        android:label="@com.globalmentor.android:string/app_applicationlistactivity_label"
        android:name="com.globalmentor.android.app.ApplicationListActivity" >
    </activity>

这甚至无法编译.但这确实:

That doesn't even compile. But this does:

    <activity
        android:label="@string/app_applicationlistactivity_label"
        android:name="com.globalmentor.android.app.ApplicationListActivity" >
    </activity>

为什么?Android文档是什么意思它谈到package_name"?为什么第一个示例不起作用,为什么第二个示例起作用?我不希望我所有的资源名称都合并到同一个 R 文件中——我希望它们像我写的那样分成包.

Why? What does the Android documentation mean which it talks about the "package_name"? Why doesn't the first example work, and why does the second example work? I don't want all my resource names merged into the same R file---I want them partitioned into packages, like I wrote them.

推荐答案

简而言之:包名前缀是给共享库的,不是给你的apk的.

In short: package name prefix is for shared libraries, not for your apk.

文档内容如下:

要引用系统资源,您需要包含包名

To reference a system resource, you would need to include the package name

这对应于外部共享库(及其资源),您的应用程序被链接到(例如地图).它们有自己的 R 类,没有与您的应用程序合并.

This corresponds for external shared libraries (and their resources), your application is linked against (e.g. maps). They have their own R class, not merged with that of your application.

这些都是你在 android manifest 部分提到的标准 android 包和共享库.

These are all standard android packages and shared libraries you mentioned in section of android manifest.

正如 Nikolay 指出的那样,所有应用程序资源都被合并,这就是为什么大多数库项目使用 abs__ 之类的前缀作为其资源名称的原因.

As Nikolay pointed out, all app resources are merged, that is why the majority library projects use prefixes like abs__ for their resource names.

有关更多信息,请参阅图书馆项目文档.

See Library projects doc for additional info.

这篇关于如何在 Android 中使用 XML 从库中的另一个包中引用字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)