Can the Android Layout folder contain subfolders?

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 heavy projects, then there should be a hierarchy and sub-folders needed inside the layout folder.

for eg

layout
-- layout_personal
   -- personal_detail.xml
   -- personal_other.xml
--layout_address
  -- address1.xml
  -- address2.xml

Like the same way, we would like to have sub-folders for the large application, so is there any way to do so inside the android project?

I am able to create layout-personal and layout_address sub-folders inside the layout folder, but when the time comes to access the xml layout file using R.layout._______ , at that time there is no any xml layout pop-up inside the menu.


You CAN do this with gradle. I've made a demo project showing how.

The trick is to use gradle's ability to merge multiple resource folders, and set the res folder as well as the nested subfolders in the sourceSets block.

The quirk is that you can't declare a container resource folder before you declare that folder's child resource folders.

Below is the sourceSets block from the build.gradle file from the demo. Notice that the subfolders are declared first.

sourceSets {
    main {
        res.srcDirs =
        [
                'src/main/res/layouts/layouts_category2',
                'src/main/res/layouts',
                'src/main/res'
        ]
    }
}

嵌套资源图片

Also, the direct parent of your actual resource files (pngs, xml layouts, etc..) does still need to correspond with the specification.


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.


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.

  • Copy all of the XML files out of your layout directory, and put them into a directory on the desktop or something for backup.
  • Delete the entire layout directory (Make sure you backed everything up from step 1!!!)
  • Right click the res directory and select new > directory.
  • 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).
  • 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').
  • 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).
  • Put the XML files you want inside the new 'layout' directory from the backup you made on your desktop.
  • Repeat steps 5 - 7 for as many custom directories as you desire.
  • 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'
                    ]
        }
    }
    
  • 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.

    目录结构

    链接地址: http://www.djcxy.com/p/84750.html

    上一篇: Android Studio布局错误

    下一篇: Android布局文件夹是否可以包含子文件夹?