在Android SDK文档中,所有使用@drawable/my_image xml语法的示例都直接处理存储在项目res/drawable目录中的图像。

我想知道在可绘制目录中创建子目录是否显式不允许。

例如,如果我有以下目录布局:

res/drawable
-- sandwiches
  -- tunaOnRye.png
  -- hamAndSwiss.png
-- drinks
  -- coldOne.png
  -- hotTea.png

我可以引用金枪鱼沙拉三明治的图片@drawable/三明治/tunaOnRye吗

或者我必须在可绘制目录中保持层次结构平坦。


当前回答

我写了一个eclipse插件,它允许创建虚拟子文件夹,用两个下划线__分隔文件名。该项目处于早期阶段,但不用担心它不会使您的IDE崩溃

更多细节可以在这里找到,请随意fork和发送拉请求:

https://github.com/kirill578/Android-Sorted-Res-Folder

其他回答

部分解决这个问题的一种方法是使用API Level后缀。 我使用res/layout-v1, res/layout-v2等在同一个apk中持有多个子项目。 此机制可用于所有资源类型。

显然,这只能用于当你的目标API级别高于res/layout-v?你在吸毒。

另外,要注意Android 1.5和1.6中的漏洞。 请参阅有关API Level后缀的Andoroid文档。

Gradle与Android Studio可以这样做(链接)。

在"配置结构"一段中

sourceSets {
 main {
    java {
        srcDir 'src/java'
    }
    resources {
        srcDir 'src/resources'
    }
 }
}

对于这种情况,有一个解决方案:你可以创建一个resVector(例如)文件夹,与默认的res文件夹在同一级别。在那里你可以添加任何drawable-xxx资源文件夹:

resVector
-drawable
-layout
-color

之后你所需要做的就是添加

sourceSets {
        main.res.srcDirs += 'src/main/resVector'
    }

进入你的构建。Gradle文件(在android{}内)。

Right click on Drawable Select New ---> Directory Enter the directory name. Eg: logo.png(the location will already show the drawable folder by default) Copy and paste the images directly into the drawable folder. While pasting you get an option to choose mdpi/xhdpi/xxhdpi etc for each of the images from a list. Select the appropriate option and enter the name of the image. Make sure to keep the same name as the directory name i.e logo.png Do the same for the remaining images. All of them will be placed under the logo.png main folder.

不,资源机制不支持可绘制目录中的子文件夹,所以是的-你需要保持层次结构平坦。

您所显示的目录布局将导致所有图像都不可用。

从我自己的实验来看,在res/drawable文件夹中有一个包含任何项目的子文件夹,将导致资源编译器失败——阻止R.java文件正确生成。