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

时间:2023-01-31
本文介绍了如何在 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 包结构最佳实践 下一篇:如何使用 Android API 21 Lollipop 获取最近的应用程序列表?

相关文章

最新文章