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

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

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

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

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

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


当前回答

在main中创建一个文件夹。 如:“res_notification_btn”

并创建树形文件夹。比如" drawable "或" layout "

然后在build中。Gradle '添加这个

sourceSets
            {
                main
                {
                    res
                    {
                        srcDirs = ['src/main/res_notification_btn', 'src/main/res']
                      or
                        srcDir 'src/main/res_notification_btn'
                    }
                }
            }

其他回答

我喜欢使用一个简单的脚本来将设计人员提供的有组织的目录结构平铺成可以用来生成R文件的东西。

在drawable-hdpi中使用当前路径运行:

#! /bin/bash
DIRS=`find * -type d`
for dir in ${DIRS} ; do 
  for file in `ls ${dir}` ; do
    mv ${dir}/${file}  ${dir}_${file};
  done 
  rmdir ${dir};
done

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.

#!/usr/bin/env ruby

# current dir should be drawable-hdpi/ etc

# nuke all symlinks
Dir.foreach('.') {|f|
    File.delete(f) if File.symlink?(f)
}

# symlink all resources renaming with underscores
Dir.glob("**/*.png") {|f|
    system "ln -s #{f} #{f.gsub('/', '_')}" if f.include?("/")
}

在main中创建一个文件夹。 如:“res_notification_btn”

并创建树形文件夹。比如" drawable "或" layout "

然后在build中。Gradle '添加这个

sourceSets
            {
                main
                {
                    res
                    {
                        srcDirs = ['src/main/res_notification_btn', 'src/main/res']
                      or
                        srcDir 'src/main/res_notification_btn'
                    }
                }
            }

实际上,在Android Studio上这是可能的。你可以有嵌套资源,如下所示:

这里还有一个插件来对资源进行分组。

但我建议避免这样做。