Can the Android layout folder contain subfolders?

前端 未结 20 2631
暗喜
暗喜 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:44

    Cannot have subdirectories (easily) but you can have additional resource folders. Surprised no one mentioned it already, but to keep the default resource folders, and add some more:

        sourceSets {
            main.res.srcDirs += ['src/main/java/XYZ/ABC']
        }
    
    0 讨论(0)
  • 2020-11-22 03:45

    Top answers have several disadvantages: you have to add new layout paths, AS places new resources to res\layouts folder instead of res\values.

    Combining several answers I wrote similar:

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

    I made folders with this article: http://alexzh.com/tutorials/how-to-store-layouts-in-different-folders-in-android-project/. In order to create subfolders you should use this menu: New > Folder > Res Folder.

    UPDATE

    After a couple of weeks I found that changes in resources are not noticed by Android Studio. So, some weird bugs appear. For instance, layouts continue to show old sizes, margins. Sometimes AS doesn't find new XML-files (especially during run-time). Sometimes it mixes view ids (references to another XML-file). It's often required to press Build > Clean Project or Build > Rebuild Project. Read Rebuild required after changing xml layout files in Android Studio.

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

    I use Android File Grouping plugin for Android Studio.It doesn't really allows you to create sub-folders, but it can DISPLAY your files and resources AS they are in different folders. And this is exactly what I wanted.

    You can install "Android File Grouping" plugin by

    Windows:

    Android Studio -> File -> Settings -> Plugins.

    Mac:

    Android Studio -> Android Studio Tab (Top Left) -> Preferences -> Plugins -> Install JetBrains Plugin..

    For Mac, I was able to test it and was not able to search for the plugin. So I downloaded the plugin from here and used the Install plugin from disk option from the above setting.

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

    Now we can easily do with JetBrains plugin called "Android File Grouping"

    check out this link

    Android File Grouping

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

    Well, the short answer is no. But you definitely can have multiple res folders. That, I think, is as close as you can get to having subfolders for the layout folder. Here's how you do it.

    0 讨论(0)
  • 2020-11-22 03:47
    • Step 1: Right click on layout - show in explorer
    • Step 2: Open the layout folder and create the subfolders directly: layout_1, layout_2 ...
    • Step 3: open layout_1 create folder layout (note: mandatory name is layout), open layout_2 folder create layout subdirectory (note: mandatory name is layout) ...
    • Step 4: Copy the xml files into the layout subdirectories in layout_1 and layout_2
    • Step 5: Run the code in buid.grade (module app) and hit sync now:

    sourceSets {
        main {
            res.srcDirs =
                [
                    'src / main / res / layout / layout_1'
                    'src / main / res / layout / layout_2',
                    'src / main / res'
                ]
        }
    }
    
    • Step 6: Summary: All the steps above will only help clustering folders and display in 'project' mode, while 'android' mode will display as normal.
    • So I draw that maybe naming prefixes is as effective as clustering folders.
    0 讨论(0)
提交回复
热议问题