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.
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布局错误