Can the Android drawable directory contain subdirectories?

后端 未结 21 970
醉梦人生
醉梦人生 2020-11-22 04:13

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

相关标签:
21条回答
  • 2020-11-22 04:44

    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/drawable folder, will cause the resource compiler to fail -- preventing the R.java file from being generated correctly.

    0 讨论(0)
  • 2020-11-22 04:45

    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:

    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 too badly compared to the folder structure equivalent.

    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 :)

    0 讨论(0)
  • 2020-11-22 04:48

    assets/ You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data. http://developer.android.com/tools/projects/index.html

    0 讨论(0)
  • 2020-11-22 04:50

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

    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.

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

    0 讨论(0)
  • 2020-11-22 04:50

    This is not perfect methods. You have to implement same way which is display here.

    You can also call the image under the folder through the code you can use

    Resources res = getResources();
    Drawable shape = res. getDrawable(R.drawable.gradient_box);
    
    TextView tv = (TextView)findViewByID(R.id.textview);
    tv.setBackground(shape);
    
    0 讨论(0)
  • 2020-11-22 04:51

    Subdirectories are not allowed, the resource must contain only [a-z0-9_.].

    No you have uppercase letters, and no forward slashes.

    0 讨论(0)
提交回复
热议问题