现在,我在'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布局弹出。


当前回答

答案是否定的。

我想提请您注意这本书Pro Android 2,它指出:

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 think the most elegant solution to this problem (given that subfolders are not allowed) is to prepend the file names with the name of the folder you would have placed it inside of. For example, if you have a bunch of layouts for an Activity, Fragment, or just general view called "places" then you should just prepend it with places_my_layout_name. At least this solves the problem of organizing them in a way that they are easier to find within the IDE. It's not the most awesome solution, but it's better than nothing.

不可能,但是布局文件夹是按名称排序的。因此,我在布局文件名前加上包名。例如,对于“购买”和“玩”两个包:

buying_bought_tracks.xml
buying_buy_tracks.xml
playing_edit_playlist.xml
playing_play_playlist.xml
playing_show_playlists.xml

现在有了Android Studio和Gradle,你可以在你的项目中有多个资源文件夹。允许组织不仅你的布局文件,但任何类型的资源。

它不是一个确切的子文件夹,但可以分离应用程序的部分。

配置如下:

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

检查文档。

如果你在批准的答案中使用了这个方法,并且想要稍微改进一下,那么就像这样更改gradle设置:

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

因此,如果你添加更多的文件夹和布局,你不需要回到这里,并附加一个很长的源文件夹列表,让gradle为你获取所有的文件夹。

@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。