从其他资源文件夹获取可绘制对象

Get drawable from other resource folder

我有一个这样的项目结构:

文件夹 "coffee_types" 是一个 Android 类型为 drawable 的资源文件夹,创建方式如下:

以前,当我只使用主要的 drawables 文件夹时,我使用这个函数来获取 Drawable 对象:

private Drawable getDrawableByName(String name) {
    return getResources().getDrawable(getResources().getIdentifier(name, "drawable", getPackageName()));
}

我试图更改它以使其与 "coffee_types" 文件夹一起使用,但没有成功。

感谢任何帮助。

The folder "coffee_types" is an Android resource folder with type drawable

不,不是。您不能发明自己的资源类型。

无法从另一个目录访问可绘制对象。
但是您可以将图片命名为:"coffee_image_1"、"coffee_image_2" 等(就像您所做的那样)并将它们放入drawables 文件夹。
这样会更容易找到它们。