使用 Android 库项目时如何正确引用 xml 资源?

发布于 2024-10-15 14:51:04 字数 663 浏览 2 评论 0原文

我有一个 Android 库项目,其中包含定义的多种样式/主题在其 styles.xml 文件中。

通常,当在其他 xml 布局或清单文件中引用此样式时,您只需编写 android:theme="@style/TheNameOfTheStyle"

当使用库项目时,依赖项目如何正确引用该样式现在的风格?只要将其保留为 android:theme="@style/TheNameOfTheStyle" 就会出现错误,提示找不到与该名称匹配的资源。

我想这个同样的问题也适用于其他引用,如@string,@color等。

谢谢

编辑:好的,看来你应该像平常一样引用它,但是eclipse中的控制台给了我这个错误:

错误:找不到匹配的资源 给定名称(在“主题”处,值为“@style/ThemeName”)。

(捂脸)编辑

我只需要在 Eclipse 中“清理项目”。现在它正在发挥作用。

I have an Android Library Project that contains several styles/themes defined in its styles.xml file.

Normally, when referencing this style in some other xml layout or manifest file, you would just write android:theme="@style/TheNameOfTheStyle"

When using a library project, how does the dependent project properly reference that style now? Just leaving it as android:theme="@style/TheNameOfTheStyle" gives me an error saying there is no resource found matching that name.

I guess this same question applies for other references like @string, @color etc.

Thanks

Edit: Ok so it appears that you are suppose to reference it just like normal, but the console in eclipse is giving me this error:

Error: No resource found that matches
the given name (at 'theme' with value '@style/ThemeName').

(facepalm) Edit

I just needed to "Clean Project" in Eclipse. Now it's working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

债姬 2024-10-22 14:51:04

在添加资源(或仅打开自动构建)后构建项目很重要,并且清理项目在大多数情况下都会有所帮助。当“出现问题”时重新启动 Eclipse 也是一个好主意。

另请注意,Android 会合并资源,因此最后构建的项目“赢得”了该名称。因此,最好以某种方式为所有资源添加前缀。

Its important to build the project after adding resources (or just have autobuild on) and cleaning the project helps most of the times. Restarting Eclipse when "something" go wrong is a good idea, too.

Also note that Android merges the recources, so the last project that is build "wins" the name. For this reason it's a good idea to prefix ALL resources somehow.

忱杏 2024-10-22 14:51:04

我使用 IntelliJ,对我有用的是正常引用资源并确保将库添加为项目的依赖项

I use IntelliJ and what worked for me is to reference the resource normally and to make sure the library is added as a dependency of the project

风铃鹿 2024-10-22 14:51:04

我刚刚遇到了同样的问题,但是在我将库项目和常规项目设置为具有匹配的:目标构建属性之前,在 Eclipse 中进行清理不起作用。

I just ran into the same issue however cleaning in Eclipse did not work until I set both the library project and the regular project to have a matching : Target Build Property.

沉溺在你眼里的海 2024-10-22 14:51:04

我刚刚在 Android Studio 中遇到了这个问题。尽管 project.properties 声明 android.library=true,Android Studio 似乎并没有意识到我的库项目是一个库。

我通过选中项目结构中的“Is Library”复选框解决了这个问题。

在此处输入图像描述

I just had this issue in Android Studio. It seems Android Studio didn't realise my library project was a library despite the project.properties stating android.library=true.

I fixed this issue by checking the "Is Library" checkbox in Project Structure.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文