Is it possible to use sub folders in drawables in Android? [duplicate]

扶醉桌前 提交于 2019-12-05 16:34:09

问题


In my application I have several folders and subfolders of images. They are inside drawables. How can I retrieve images from drawables subfolders?

Example:

drawable>actions>sports>soccer.png

How can I get this "soccer.png" photo?

Any help is appreciated.

Thanks!


回答1:


No, the android resources mechanism doesn't support subfolders in the drawable directory, you can't put it.

I think if you having a subfolder with any items in it, within the res/drawable folder, will cause the resource compiler to fail -- preventing the R.java file from being generated correctly.

The only one thing is put the images in flat names like, drawable_actions_sports_soccer.png.




回答2:


This is now (sort of) possible by using Android Studio and Gradle.

Whilst subfolders are still not possible, it is possible to separate resources into different sets and have them merged by the build system.

As an example, to simplify my project, I wanted to keep theme specific drawables separate from standard drawables.

I created a new resource folder named 'res_lighttheme' as illustrated in the picture below.

I then added a pointer to this folder to my build.gradle file like this. To add more than one just add another line to the definition.

The result is that BOTH folders are then considered to be valid destinations for resources. A build error will be generated if there is a conflict. i.e if the same resource is included in both folders.



来源:https://stackoverflow.com/questions/9029051/is-it-possible-to-use-sub-folders-in-drawables-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!