Android drawable 目录可以包含子目录吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1077357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:39:42  来源:igfitidea点击:

Can the Android drawable directory contain subdirectories?

androiddrawableandroid-resources

提问by Pepper Lebeck-Jobe

In the Android SDK documentation, all of the examples used with the @drawable/my_image xml syntax directly address images that are stored in the res/drawable directory in my project.

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

I am wondering if it is explicitly not okay to create a sub directory within the drawable directory.

我想知道在 drawable 目录中创建子目录是否明确是不行的。

For example, if I had the following directory layout:

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

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

Could I reference the image of a tuna salad sandwich as @drawable/sandwiches/tunaOnRye

我可以将金枪鱼沙拉三明治的图像引用为 @drawable/sandwiches/tunaOnRye

Or do I have to keep the hierarchy flat in the drawable directory.

或者我是否必须在 drawable 目录中保持层次结构平坦。

回答by Reto Meier

No, the resources mechanism doesn't support subfolders in the drawable directory, so yes - you need to keep that hierarchy flat.

不,资源机制不支持可绘制目录中的子文件夹,所以是的 - 您需要保持该层次结构平坦。

The directory layout you showed would result in none of the images being available.

您显示的目录布局将导致没有可用的图像。

From my own experiments it seems that having a subfolder with any items in it, within the res/drawablefolder, will cause the resource compiler to fail -- preventing the R.javafile from being generated correctly.

从我自己的实验来看,似乎在该res/drawable文件夹内有一个包含任何项目的子文件夹,将导致资源编译器失败——阻止R.java正确生成文件。

回答by Cheezmeister

The workaround I'm using (and the one Android itself seems to favor) is to essentially substitute an underscore for a forward slash, so your structure would look something like this:

我正在使用的解决方法(Android 本身似乎更喜欢)基本上是用下划线代替正斜杠,因此您的结构将如下所示:

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

The approach requires you to be meticulous in your naming and doesn't make it much easier to wrangle the files themselves (if you decided that drinks and sandwiches should really all be "food", you'd have to do a mass rename rather than simply moving them to the directory); but your programming logic's complexity doesn't suffer toobadly compared to the folder structure equivalent.

该方法要求您在命名时一丝不苟,并且不会让文件本身更容易纠缠(如果您决定饮料和三明治真的应该全部为“ food”,则必须进行批量重命名,而不是简单地将它们移动到目录中);但是与等效的文件夹结构相比,您的编程逻辑的复杂性并没有受到太大影响。

This situation sucks indeed. Android is a mixed bag of wonderful and terrible design decisions. We can only hope for the latter portion to get weeded out with all due haste :)

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

回答by android developer

Actually, on Android Studio it is possible. You can have nested resources as shown here:

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

enter image description here

在此处输入图片说明

There is also a plugin to group resources here.

还有一个插件组资源在这里

回答by dijipiji

Yes - it does suck :) However you can use the assets folder and have sub directories in there and load images that way.

是的 - 它确实很糟糕:) 但是您可以使用资产文件夹并在其中设置子目录并以这种方式加载图像。

回答by milkia

Use assets folder.

使用资产文件夹。

sample code:

示例代码:

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

image = BitmapFactory.decodeStream(is);

回答by Kirill Kulakov

I've wrote an eclipse plugin which allows to create virtual subfolder by separating the file name with two underscores __. The project is in early stages, but don't worry it won't crash your IDE

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

more details can be found here, feel free to fork and send pull requests:

可以在此处找到更多详细信息,请随时分叉并发送请求请求:

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

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

enter image description here

在此处输入图片说明

回答by Alec Holmes

In android studio with gradle you can have multiple source directors which will allow you to separate resources. For example:

在带有 gradle 的 android studio 中,您可以拥有多个源控制器,这将允许您分离资源。例如:

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

However the names must not collide which means you will still need to have names such as sandwiches_tunaOnRye but you will be able to have a seperate section for all of your sandwiches.

但是,名称不能冲突,这意味着您仍然需要使用诸如sandwiches_tunaOnRye 之类的名称,但您将能够为所有三明治创建一个单独的部分。

This allows you to store your resources in different structures (useful for auto generated content such as actionbargenerator)

这允许您将资源存储在不同的结构中(对于自动生成的内容如 actionbargenerator 很有用)

回答by Rene

I like to use a simple script to flatten an organized directory structure provided by designers to something that can be used to generate an R file.

我喜欢使用一个简单的脚本将设计师提供的有组织的目录结构展平为可用于生成 R 文件的内容。

Run with current path in drawable-hdpi:

在 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

回答by shi

With the advent of library system, creating a library per big set of assets could be a solution.

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

It is still problematic as one must avoid using the same names within all the assets but using a prefix scheme per library should help with that.

它仍然存在问题,因为必须避免在所有资产中使用相同的名称,但使用每个库的前缀方案应该会有所帮助。

It's not as simple as being able to create folders but that helps keeping things sane...

这并不像能够创建文件夹那么简单,但这有助于保持头脑清醒......

回答by OferR

One way to partially get around the problem is to use the API Level suffix. I use res/layout-v1, res/layout-v2 etc to hold multiple sub projects in the same apk. This mechanism can be used for all resource types.

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

Obviously, this can only be used if you are targeting API levels above the res/layout-v? you are using.

显然,只有当你的目标 API 级别高于 res/layout-v? 您正在使用。

Also, watch out for the bug in Android 1.5 and 1.6. See Andoroid documentation about the API Level suffix.

另外,请注意 Android 1.5 和 1.6 中的错误。 请参阅有关 API 级别后缀的 Andoroid 文档