替换(或“覆盖”)Android 库项目中的字符串
我一直在尝试制作一个 android 库项目,虽然构建过程运行良好,但在替换使用该库的项目中的资源时遇到了一些麻烦。
在我的库中,我有:
A
library_layout.xml
包含调用的java文件
((TextView)this.findViewById(R.id.str_my_string)).setText(R.string.my_string);
包含
的资源strings.xml
占位符 ;
在使用该库的项目中,我有
资源
strings.xml
包含实际字符串内容
我期望的行为是,当我使用库运行项目时,文本视图显示实际字符串内容 ,但它实际上包含false。
查看使用该库的应用程序,我确实看到两个R
文件,并且它们都有R.string.my_string
并且都它们等于相同的数值。
I've been trying to make an android library project, and while the build process works fine, I've been running into some trouble with replacing a resource in the project which uses the library.
In my library I have:
A
library_layout.xml
containing<TextView android:id="@+id/str_my_string" android:layout_width="wrap_content" android:layout_text="wrap_content">
A java file which calls
((TextView)this.findViewById(R.id.str_my_string)).setText(R.string.my_string);
A resource
strings.xml
containing<string name="my_string">Placeholder</string>
In the project using the library I have
A resource
strings.xml
containing<string name="my_string">Actual string content</string>
The behavior I expect is that when I run the project using the library, the text view displays Actual string content, but it actually contains false.
Looking in the app which uses the library, I do see two R
files, and both of them have R.string.my_string
and both of those are equal to the same numeric value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有同样的安排,这对我来说正如预期的那样。
该库具有布局/类,其中引用了字符串资源:
该库在其 strings.xml 中提供了默认值:
主应用程序在其 strings.xml 中具有此值:
当我在主应用程序中为此资源提供值时strings.xml,当应用程序运行时,我看到“客户端自”,当我从主应用程序 strings.xml 中删除它时,我看到库中的值“学生自”。
根据我在这里的阅读,这似乎是预期的行为:
http://developer.android.com/tools/sdk/eclipse-adt.html
上面链接中的相关引用:
I have the same arrangement and this works for me as expected.
The library has layout/class with this reference to a string resource:
The library provides a default value in its strings.xml:
The main app has this value in its strings.xml:
When I give a value for this resource in the main apps strings.xml, I see "Client Since" when the app runs, when I delete it from the main apps strings.xml, I see the value from the library, "Student Since".
Seems this is expected behaviour based on my reading here:
http://developer.android.com/tools/sdk/eclipse-adt.html
Relevant quote from the link above:
刚刚出现此错误,以下是我的情况:
如果您在库中针对不同的屏幕尺寸使用多个
strings.xml
文件,您的应用程序可能会使用该库文件。IE,如果您有 :
和 :
并且您尝试在 600dp 屏幕尺寸(即平板电脑)上启动您的应用程序,您的应用程序将从您的 获取
strings.xml(sw600dp)
>图书馆。为了避免这种情况,您可能需要在应用中创建特定于
sw600dp
分辨率的strings.xml
,其中包含与中相同的字符串值你的图书馆。Just had this error, here is what happened in my case :
If you are using multiple
strings.xml
file for different screen sizes in your library, your app might use the library file.I.E, if you have :
and :
And you try to launch your app on a 600dp screen size (i.e a tablet), your app is going to get the
strings.xml(sw600dp)
from your library.To avoid this, you might want to create a
strings.xml
specific tosw600dp
resolutions in your app, that contains the same strings values than in your library.