替换(或“覆盖")Android 库项目中的字符串 [英] Replace (or "Override") String in Android Library Project

查看:39
本文介绍了替换(或“覆盖")Android 库项目中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 一个包含

  1. A library_layout.xml containing

<TextView  
    android:id="@+id/str_my_string"  
    android:layout_width="wrap_content"  
    android:layout_text="wrap_content">

  • 一个调用的java文件

  • 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

    1. 包含

    <string name="my_string">Actual string content</string>
    

    我期望的行为是,当我使用库运行项目时,文本视图显示实际字符串内容,但它实际上包含false.

    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.

    查看使用该库的应用程序,我do看到两个 R 文件,它们都有 R.string.my_string并且两者都等于相同的数值.

    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.

    推荐答案

    我有同样的安排,这对我来说如预期的一样.

    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">
    

    库在它的 strings.xml 中提供了一个默认值:

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

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

    主应用在它的 strings.xml 中有这个值:

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

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

    当我在主应用程序 strings.xml 中为该资源指定一个值时,我看到自客户端"当应用程序运行时,当我从主应用程序 strings.xml 中删除它时,我会看到库中的值Student since".

    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".

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

    Seems this is expected behaviour based on my reading here: http://developer.android.com/tools/sdk/eclipse-adt.html

    以上链接的相关引用:

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

    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.

    这篇关于替换(或“覆盖")Android 库项目中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆