android - 无法在应用程序中使用 png 资源覆盖库的 xml 资源?

标签 android resources library-project

我有一个 Android 库 MyLib,其中包含我的应用所需的一切(针对 Android 2.2)。这个库有一个 XML 资源:

drawable/main_background.xml

在我的应用程序 MyApp 项目中,我引用了 MyLib。在这里,我想覆盖特定资源(即品牌)。所以我在 MyApp 中添加了背景图片:

drawable/main_background.png

Eclipse 一直给我这个错误:

[com.mycom.mylib.myapp] res\drawable\main_background.xml:0: error: Resource entry main_background is already defined.
[com.mycom.mylib.myapp] res\drawable\main_background.png:0: Originally defined here.

如何覆盖库项目中的资源?

最佳答案

您不能简单地使用 Android SDK 中具有不同扩展名的文件覆盖资源 ID(它是您要覆盖的资源 ID,而不是实际文件)。但是,您可以通过放入具有相同名称 (main_background.xml) 的项目 xml 文件并以正确的方式填充它以显示您的新文件 (main_background.png),您需要提前重命名。您需要的所有语法都在这里描述:

http://developer.android.com/guide/topics/resources/drawable-resource.html

,在你的情况下,它可能很简单(假设你把它作为 main_background.xml 放在你的非库项目中,并且你有你的新 png 作为 main_background_new.png):

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/main_background_new" />

通过上述解决方案,您可以从您的项目中引用 @drawable/main_background,它应该使用该项目中包含的文件,而不是库文件。

关于android - 无法在应用程序中使用 png 资源覆盖库的 xml 资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10055926/

相关文章:

android - Android 中具有交替资源的 ListView

android - 库项目和子项目中的布局 xml

android - 将 "Android Library Project"包含的 jar 暴露给引用它们的项目

android - 基于 Activity 的应用程序可以没有 Activity 吗?

android - 如何在Android中通过NFC传输数据

android - android.graphics.bitmapfactory.nativedecodestream 内存不足错误

c# - 使用外部文件或资源?

database - 为什么在一个请求中建立多个数据库连接是一种不好的做法?

c# - 将位图设置为 Xamarin.Android 中 byteArray 的图像,UI 线程上的负载最小

Android 设备监视器不显示文件