【问题标题】:Look for Resources in another Module from XML File - Android从 XML 文件中查找另一个模块中的资源 - Android
【发布时间】:2020-05-18 15:33:25
【问题描述】:

我的项目作为多个模块。本质上,将有跨多个模块使用的资源。我们称这个资源为 logo_image.png

目前,在每个模块的 res 文件夹中,都有一个 logo_image.png 的副本。这可能是个问题,因为它会占用大量空间。

我想做的是创建一个 Common 模块,它存储整个项目中使用的所有资源,并且所有引用都指向该 Common 模块中的资源。这样的事情可能吗?这是我的尝试(失败)

activity_main.xml

<ImageView
    android:id="@+id/imageView"
    android:layout_width="300dp"
    android:layout_height="150dp"
    android:src="@common/drawable/logo_image" />   <-- "Unknown reference

【问题讨论】:

    标签: android xml android-resources


    【解决方案1】:

    您可以在您的项目中创建一个名为assets 或您想要的任何其他名称的模块。然后在需要访问公共资源的每个模块中,将该模块作为依赖项包含在模块build.gradle

    implementation project(':assets')
    

    然后,您可以像在每个模块中本地定义资产一样访问资产。

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="300dp"
        android:layout_height="150dp"
        android:src="@drawable/logo_image" /> 
    

    【讨论】:

      【解决方案2】:

      如果你想使用模块来保存公共资源,只需像这样将这个插件添加到这个模块的 build.gradle 文件中

      plugins {
         id 'com.android.library'
      }
      

      或者替换 com.android.application 如果它已经在那里定义了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-22
        • 2020-09-25
        • 1970-01-01
        • 1970-01-01
        • 2017-04-20
        • 1970-01-01
        相关资源
        最近更新 更多