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

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

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

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

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

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


当前回答

#!/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?("/")
}

其他回答

在android studio和gradle中,你可以有多个源导演,这将允许你分离资源。例如:

android {
    ....
    android.sourceSets {
        main.res.srcDirs = ['src/main/extraresdirnamed_sandwiches', 'src/main/res']
    }
    ....
}

然而,名称不能冲突,这意味着你仍然需要有名称,如三明治_tunaonrye,但你将能够有一个单独的部分,为所有的三明治。

这允许你存储你的资源在不同的结构(有用的自动生成的内容,如actionbargenerator)

这是不完美的方法。你必须以同样的方式实现,就像这里显示的那样。

还可以通过使用的代码调用文件夹下的图像

Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);

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.

随着图书馆系统的出现,为大量资产创建一个图书馆可能是一种解决方案。

这仍然是有问题的,因为必须避免在所有资产中使用相同的名称,但每个库使用前缀方案应该有助于解决这一问题。

这并不像能够创建文件夹那么简单,但这有助于保持事情的理智……

不是我的,但我在寻找这个问题时发现了这个线程,如果你使用Android Studio和Gradle构建系统,它很容易,不需要插件,只需要一点点构建文件编辑

https://stackoverflow.com/a/22426467/618419