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

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

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

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

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

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


当前回答

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

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

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

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

其他回答

对于任何使用Xamarin的人(或Xamarin。Android或Xamarin.Forms),有一种方法可以做到这一点。

在Android项目的.csproj文件中找到MonoAndroidResourcePrefix的行(这里有文档,尽管很差)。在这里添加您想要使用的子目录,用分号分隔每个条目。在构建时,Visual Studio会剥离这些前缀,以便所有资源最终都在一个扁平的层次结构中。在进行这些更改之后,您可能需要重新加载解决方案。

这些目录不需要是项目中默认Resources目录的子目录。

确保你添加的文件的构建动作设置为“AndroidResource”。

Xamarin的。Android,可视化编辑器将不识别图像,并将显示错误“此资源URL无法解析”,但项目将构建,图像将在运行时可见。

使用资产文件夹。

示例代码:

InputStream is = null;
try {
    is = this.getResources().getAssets().open("test/sample.png");
} catch (IOException e) {
    ;
}

image = BitmapFactory.decodeStream(is);

不允许子目录,资源必须只包含[a-z0-9_.]。

不,你有大写字母,没有正斜杠。

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

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

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

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.