替换(或“覆盖”)Android 库项目中的字符串

发布于 2024-10-03 22:38:09 字数 994 浏览 0 评论 0原文

我一直在尝试制作一个 android 库项目,虽然构建过程运行良好,但在替换使用该库的项目中的资源时遇到了一些麻烦。

在我的库中,我有:

  1. A library_layout.xml 包含

    
    
  2. 调用的java文件

    ((TextView)this.findViewById(R.id.str_my_string)).setText(R.string.my_string);
    
  3. 包含

    的资源strings.xml
    占位符;
    

在使用该库的项目中,我有

  1. 资源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:

  1. A library_layout.xml containing

    <TextView  
        android:id="@+id/str_my_string"  
        android:layout_width="wrap_content"  
        android:layout_text="wrap_content">
    
  2. A java file which calls

    ((TextView)this.findViewById(R.id.str_my_string)).setText(R.string.my_string);
    
  3. A resource strings.xml containing

    <string name="my_string">Placeholder</string>
    

In the project using the library I have

  1. 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 技术交流群。

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

发布评论

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

评论(2

谎言 2024-10-10 22:38:09

我有同样的安排,这对我来说正如预期的那样。

该库具有布局/类,其中引用了字符串资源:

<TextView android:id="@+id/studentSinceLabel">

该库在其 strings.xml 中提供了默认值:

<string name="studentSinceLabel">Student Since</string>

主应用程序在其 strings.xml 中具有此值:

<string name="studentSinceLabel">Client Since</string>

当我在主应用程序中为此资源提供值时strings.xml,当应用程序运行时,我看到“客户端自”,当我从主应用程序 strings.xml 中删除它时,我看到库中的值“学生自”。

根据我在这里的阅读,这似乎是预期的行为:
http://developer.android.com/tools/sdk/eclipse-adt.html

上面链接中的相关引用:

在应用程序和库中都定义了资源 ID 的情况下,这些工具可确保应用程序中声明的资源具有优先级,并且库项目中的资源不会编译到应用程序 .apk 中。这使您的应用程序可以灵活地使用或重新定义任何库中定义的任何资源行为或值。

I have the same arrangement and this works for me as expected.

The library has layout/class with this reference to a string resource:

<TextView android:id="@+id/studentSinceLabel">

The library provides a default value in its strings.xml:

<string name="studentSinceLabel">Student Since</string>

The main app has this value in its strings.xml:

<string name="studentSinceLabel">Client Since</string>

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:

In the cases where a resource ID is defined in both the application and the library, the tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. This gives your application the flexibility to either use or redefines any resource behaviours or values that are defined in any library.

卖梦商人 2024-10-10 22:38:09

刚刚出现此错误,以下是我的情况:

如果您在库中针对不同的屏幕尺寸使用多个 strings.xml 文件,您的应用程序可能会使用该库文件。

IE,如果您有 :

LIBRARY :  
    res/values/strings.xml  
    res/values-sw600dp/strings.xml

和 :

APP :
    res/values/strings.xml

并且您尝试在 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 :

LIBRARY :  
    res/values/strings.xml  
    res/values-sw600dp/strings.xml

and :

APP :
    res/values/strings.xml

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 to sw600dp resolutions in your app, that contains the same strings values than in your library.

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