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

问题描述 投票:29回答:1

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

在我的图书馆中,我有:

  1. 包含library_layout.xml

    <TextView  
        android:id="@+id/str_my_string"  
        android:layout_width="wrap_content"  
        android:layout_text="wrap_content">
    
  2. 一个调用的Java文件

    ((TextView)this.findViewById(R.id.str_my_string)).setText(R.string.my_string);
    
  3. [资源strings.xml包含

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

在使用库的项目中

  1. [资源strings.xml包含

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

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

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

android android-layout eclipse-adt android-library
1个回答
63
投票

我有相同的安排,这对我来说符合预期。

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

<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中。这使您的应用程序可以灵活使用或重新定义任何库中定义的任何资源行为或值。

© www.soinside.com 2019 - 2024. All rights reserved.