在Android Studio中,可以将布局xml文件分组到文件夹中吗?
能够通过创建不同的包/文件夹来处理java文件,布局文件是否可以进行类似的分组/组织?
当我有大量的xml文件可供使用时,在我的工作流程中寻找组织的便利性。
答案 0 :(得分:4)
不,文件必须位于预定义的文件夹中。
以下是有关可用文件夹名称的更多信息的链接: http://developer.android.com/guide/topics/resources/providing-resources.html
答案 1 :(得分:1)
Actually, they can!
Although the steps are a bit too many, it is indeed possible to group your xml files in folders. Just follow the following steps:
Switch to Project view; you can now see all your project folders and sub-folders properly.
Backup all your layout files; at this stage you'll find them all under android/app/src/main/res/layout.
Delete the entire layout directory (android/app/src/main/res/layout). Remember to properly backup all your layout files before this step.
Right click the res directory and select "New" and then select "Directory".
Give it a name; remember the name must be entirely lowercase. Your new directory will now appear under the res folder. Let's call this directory layouts for instance.
Right click your new directory (layouts) and select "New" and then select "Directory". This way, we are creating a new sub-directory. We can give it any name we want, just remmber it must be completely lowercase.
You can repeat step 6 as many times as you want and keep creating sub-directories.
VERY IMPORTANT! Right click any of the sub-directories and select "New" and then select "Directory". YOU MUST NAME THIS DIRECTORY layout
.
Repeat this for all sub-directories.
Move all the backed-up xml files (in step 2) to the layout
directory of the folder you want to put them in.
Add the code below to your build.gradle (app) file:
sourceSets {
main {
res.srcDirs =
[
'src/main/res/layouts/layout_for_fragment',
'src/main/res/layouts',
'src/main/res'
]
}
}
Replace layouts with your sub-directory name and add as many sub-directories as are available.
And that's it.. Pretty herculean but it can come in handy anytime. You may want to visit this link for more clarification.
I hope this helps.. Merry coding!