现在,我在'res/layout'文件夹内存储每个XML布局文件,因此管理小型项目是可行和简单的,但当有大型和繁重的项目的情况下,那么应该有一个层次结构和子文件夹内需要的布局文件夹。

如。

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

同样,我们希望大型应用程序有子文件夹,那么在Android项目中有办法做到这一点吗?

我能够在布局文件夹内创建layout-personal和layout_address子文件夹,但当需要使用R.layout访问XML布局文件时。_______,当时在菜单中没有任何XML布局弹出。


当前回答

@eski给出的最上面的答案很好,但是代码使用起来并不优雅,所以我在gradle中编写了一个groovy脚本以供一般使用。它适用于所有构建类型和产品风格,不仅可以用于布局,还可以为任何其他资源类型(如drawable)添加子文件夹。下面是代码(把它放在项目级gradle文件的android块中):

sourceSets.each {
    def rootResDir = it.res.srcDirs[0]
    def getSubDirs = { dirName ->
        def layoutsDir = new File(rootResDir, dirName)
        def subLayoutDirs = []
        if (layoutsDir.exists()) {
            layoutsDir.eachDir {
                subLayoutDirs.add it
            }
        }
        return subLayoutDirs
    }
    def resDirs = [
            "anims",
            "colors",
            "drawables",
            "drawables-hdpi",
            "drawables-mdpi",
            "drawables-xhdpi",
            "drawables-xxhdpi",
            "layouts",
            "valuess",
    ]
    def srcDirs = resDirs.collect {
        getSubDirs(it)
    }
    it.res.srcDirs = [srcDirs, rootResDir]
}

实践中怎么做?

例如,我想为布局创建名为activity的子文件夹,在resDirs变量中添加任何名称的字符串,如布局,然后布局xml文件应该放在res\layouts\activity\layout\xxx.xml中。

如果我想为drawable创建名为selectors的子文件夹,在resDirs变量中添加任何名称的字符串,如drawables,那么drawable xml文件应该放在res\drawables\selectors\drawable xxx.xml中。

文件夹名称,如布局和drawables是在resDirs变量中定义的,它可以是任何字符串。 您创建的所有子文件夹,如活动或选择器,都被视为与res文件夹相同。所以在selectors文件夹中,我们必须另外创建一个drawable folder,把xml文件放到drawable folder中,这样gradle才能正常识别xml文件为drawable。

其他回答

While all the proposals for multiple resource sets may work, the problem is that the current logic for the Android Studio Gradle plug-in will not update the resource files after they have changed for nested resource sets. The current implementation attempts to check the resource directories using startsWith(), so a directory structure that is nested (i.e. src/main/res/layout/layouts and src/main/res/layout/layouts_category2) will choose src/main/res/layout/layouts consistently and never actually update the changes. The end result is that you have to rebuild/clean the project each time.

我在https://android-review.googlesource.com/#/c/157971/上提交了一个补丁,试图帮助解决问题。

你可以用gradle做到这一点。我做了一个演示项目来展示如何做到这一点。

诀窍是使用gradle的能力来合并多个资源文件夹,并设置res文件夹以及sourceSets块中的嵌套子文件夹。

奇怪的是,在声明容器资源文件夹的子资源文件夹之前,您不能声明该文件夹。

下面是构建中的sourceSets块。演示中的Gradle文件。注意,先声明子文件夹。

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

另外,实际资源文件(png、xml布局等)的直接父文件仍然需要与规范相对应。

我使用Android文件分组插件的Android工作室。它不允许你创建子文件夹,但是它可以显示你的文件和资源,因为它们在不同的文件夹中。这正是我想要的。

你可以安装“Android文件分组”插件通过

窗口:

Android Studio ->文件->设置->插件。

Mac:

Android Studio标签(左上)->首选项->插件->安装JetBrains插件..

对于Mac,我能够测试它,不能搜索插件。所以我从这里下载了插件,并从上面的设置中使用了从磁盘安装插件选项。

如果你是在linux或mac上开发,一种变通方法是,创建包含符号链接到布局文件的子文件夹。只需使用ln命令和-s

ln -s PATH_TO_YOUR_FILE

问题是,你的布局文件夹仍然包含所有的.xml文件。但是你可以通过使用子文件夹来选择它们。这是最接近你想要的东西。

我刚读到,如果你使用的是Vista或更高版本,这可能也适用于Windows。这里有一个mklink命令。只是谷歌,我从来没用过。

另一个问题是,如果你已经打开了文件,并试图再次打开它,插件抛出一个空指针异常。但它并没有挂断。

我只是想补充eskis对遇到麻烦的人的精彩回答。(注意:这只会工作,看起来像“项目”视图中的单独目录,不幸的是,不是“android”视图。)

用以下测试。 BuildToolsVersion = 23.0.0 Gradle 1.2.3 & 1.3.0

这就是我如何让我的工作与一个已经建立的项目。

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 $$$$

但是说真的. .我就是这样让它起作用的。如果有人有任何问题,请告诉我。我可以试着帮忙。

图片比文字更有价值。