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

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

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

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

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

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


当前回答

使用资产文件夹。

示例代码:

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

image = BitmapFactory.decodeStream(is);

其他回答

我使用的解决方法(Android本身似乎更喜欢)基本上是用下划线代替正斜杠,所以你的结构看起来像这样:

sandwich_tunaOnRye.png
sandwich_hamAndSwiss.png
drink_coldOne.png
drink_hotTea.png

这种方法要求您在命名时一丝不差,并且不会使文件本身变得更容易(如果您决定饮料和三明治真的都应该是“食物”,那么您必须进行大规模重命名,而不是简单地将它们移动到目录中);但是与相同的文件夹结构相比,您的编程逻辑的复杂性并没有受到太大的影响。

这种情况确实很糟糕。Android是一个优秀和糟糕设计决策的混合体。我们只能希望后一部分能尽快被淘汰:)

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

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

但我建议避免这样做。

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.

资产/ 您可以使用它来存储原始资产文件。保存在这里的文件将按原样编译为.apk文件,并保留原始文件名。您可以以与使用uri的典型文件系统相同的方式导航此目录,并使用AssetManager将文件作为字节流读取。例如,这是纹理和游戏数据的好位置。 http://developer.android.com/tools/projects/index.html

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

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

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