Can the Android layout folder contain subfolders?

前端 未结 20 2612
暗喜
暗喜 2020-11-22 03:10

Right now, I\'m storing every XML layout file inside the \'res/layout\' folder, so it is feasible and simple to manage small projects, but when there is a case of large and

相关标签:
20条回答
  • 2020-11-22 03:56

    I think the most elegant solution to this problem (given that subfolders are not allowed) is to prepend the file names with the name of the folder you would have placed it inside of. For example, if you have a bunch of layouts for an Activity, Fragment, or just general view called "places" then you should just prepend it with places_my_layout_name. At least this solves the problem of organizing them in a way that they are easier to find within the IDE. It's not the most awesome solution, but it's better than nothing.

    0 讨论(0)
  • 2020-11-22 03:57

    I just wanted to add onto eskis' fantastic answer for people having trouble. (Note: This will only work and look like separate directories inside the 'project' view, not the 'android' view unfortunately.)

    Tested with the following. BuildToolsVersion = 23.0.0 gradle 1.2.3 & 1.3.0

    This is how I got mine to work with an already built project.

    1. Copy all of the XML files out of your layout directory, and put them into a directory on the desktop or something for backup.
    2. Delete the entire layout directory (Make sure you backed everything up from step 1!!!)
    3. Right click the res directory and select new > directory.
    4. Name this new directory "layouts". (This can be whatever you want, but it will not be a 'fragment' directory or 'activity' directory, that comes later).
    5. Right click the new "layouts" directory and select new > directory. (This will be the name of the type of XML files you will have in it, for example, 'fragments' and 'activities').
    6. Right click the 'fragment' or 'activities' directory (Note: this doesn't have to be 'fragment' or 'activities' that's just what i'm using as an example) and select new > directory once again and name this directory "layout". (Note: This MUST be named 'layout'!!! very important).
    7. Put the XML files you want inside the new 'layout' directory from the backup you made on your desktop.
    8. Repeat steps 5 - 7 for as many custom directories as you desire.
    9. Once this is complete, go into your modules gradle.build file and create a sourceSets definition like this...(Make sure 'src/main/res/layouts' & 'src/main/res' are always the bottom two!!!! Like I am showing below).

      sourceSets {
          main {
              res.srcDirs =
                      [
                              'src/main/res/layouts/activities',
                              'src/main/res/layouts/fragments',
                              'src/main/res/layouts/content',
                              'src/main/res/layouts',
                              'src/main/res'
                      ]
          }
      }
      
    10. Profit $$$$

    But seriously.. this is how I got it to work. Let me know if anyone has any questions.. I can try to help.

    Pictures are worth more than words.

    0 讨论(0)
  • 2020-11-22 03:58

    The answer is no.

    I would like to draw your attention towards this book Pro Android 2 that states:

    It is also worth noting a few constraints regarding resources. First, Android supports only a linear list of files within the predefined folders under res. For example, it does not support nested folders under the layout folder (or the other folders under res).

    Second, there are some similarities between the assets folder and the raw folder under res. Both folders can contain raw files, but the files within raw are considered resources and the files within assets are not.

    Note that because the contents of the assets folder are not considered resources, you can put an arbitrary hierarchy of folders and files within it.

    0 讨论(0)
  • 2020-11-22 03:59

    Check Bash Flatten Folder script that converts folder hierarchy to a single folder

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

    A way i did it was to create a separate res folder at the same level as the actual res folder in your project, then you can use this in your apps build.gradle

    android {
        //other stuff
    
        sourceSets {
            main.res.srcDirs = ['src/main/res', file('src/main/layouts').listFiles()]
        }
    }
    

    then each subfolder of your new res folder can be something relating to each particular screen or something in your app, and each folder will have their own layout / drawable / values etc keeping things organised and you dont have to update the gradle file manually like some of these other answers require (Just sync your gradle each time you add a new resource folder so it knows about it, and make sure to add the relevant subfolders before adding your xml files).

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

    If you use the method in the approved answer, and want to improve it on a bit, then change the gradle setting like this:

        sourceSets {
        main {
            res.srcDirs = [
                file("src/main/res/layouts/").listFiles(),
                "src/main/res/layouts",
                "src/main/res"
            ]
        }
    }
    

    So if you add more folders and layouts, you don't need to come back here and append a long list of source folders, let gradle get all the folders for you.

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