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


当前回答

你可以用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 Studio和Gradle,你可以在你的项目中有多个资源文件夹。允许组织不仅你的布局文件,但任何类型的资源。

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

配置如下:

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

检查文档。

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

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

小问题

我能够通过遵循这个问题的顶部答案来实现子文件夹。

然而,随着项目越来越大,你会有很多子文件夹:

sourceSets {
    main {
        res.srcDirs = [
            'src/main/res/layouts/somethingA',
            'src/main/res/layouts/somethingB',
            'src/main/res/layouts/somethingC',
            'src/main/res/layouts/somethingD',
            'src/main/res/layouts/somethingE',
            'src/main/res/layouts/somethingF',
            'src/main/res/layouts/somethingG',
            'src/main/res/layouts/somethingH',
            'src/main/res/layouts/...many more',
            'src/main/res'
        ]
    }
}

不是什么大问题,但是:

当清单变得很长时,它就不好看了。 你必须改变你的应用/构建。每次添加新文件夹时Gradle。


改进

所以我写了一个简单的Groovy方法来抓取所有嵌套文件夹:

def getLayoutList(path) {
    File file = new File(path)
    def throwAway = file.path.split("/")[0]
    def newPath = file.path.substring(throwAway.length() + 1)
    def array = file.list().collect {
        "${newPath}/${it}"
    }
    array.push("src/main/res");
    return array
}

将此方法粘贴到android{…}块在你的app/build.gradle。


如何使用

对于这样的结构:

<project root>
├── app <---------- TAKE NOTE
├── build
├── build.gradle
├── gradle
├── gradle.properties
├── gradlew
├── gradlew.bat
├── local.properties
└── settings.gradle

像这样使用它:

android {
    sourceSets {
        main {
            res.srcDirs = getLayoutList("app/src/main/res/layouts/")
        }
    }
}

如果你有一个这样的结构:

<project root>
├── my_special_app_name <---------- TAKE NOTE
├── build
├── build.gradle
├── gradle
├── gradle.properties
├── gradlew
├── gradlew.bat
├── local.properties
└── settings.gradle

你可以这样使用它:

android {
    sourceSets {
        main {
            res.srcDirs = getLayoutList("my_special_app_name/src/main/res/layouts/")
        }
    }
}

解释

getLayoutList()接受一个相对路径作为参数。相对路径相对于项目的根路径。所以当我们输入"app/src/main/res/layouts/"时,它会以数组的形式返回所有子文件夹的名称,这将与:

[
    'src/main/res/layouts/somethingA',
    'src/main/res/layouts/somethingB',
    'src/main/res/layouts/somethingC',
    'src/main/res/layouts/somethingD',
    'src/main/res/layouts/somethingE',
    'src/main/res/layouts/somethingF',
    'src/main/res/layouts/somethingG',
    'src/main/res/layouts/somethingH',
    'src/main/res/layouts/...many more',
    'src/main/res'
]

下面是带有注释的脚本,便于理解:

def getLayoutList(path) {
    // let's say path = "app/src/main/res/layouts/
    File file = new File(path)

    def throwAway = file.path.split("/")[0]
    // throwAway = 'app'

    def newPath = file.path.substring(throwAway.length() + 1) // +1 is for '/'
    // newPath = src/main/res/layouts/

    def array = file.list().collect {
        // println "filename: ${it}" // uncomment for debugging
        "${newPath}/${it}"
    }

    array.push("src/main/res");
    // println "result: ${array}" // uncomment for debugging

    return array
}

希望能有所帮助!

第一步:在资源管理器中右键单击布局-显示 步骤2:打开布局文件夹并直接创建子文件夹:layout_1, layout_2… 步骤3:打开layout_1创建文件夹布局(注意:必选名称为布局),打开layout_2文件夹创建文件夹布局子目录(注意:必选名称为布局)… 步骤4:将xml文件复制到layout_1和layout_2的布局子目录中 步骤5:运行build中的代码。Grade(模块应用),现在点击同步:


sourceSets {
    main {
        res.srcDirs =
            [
                'src / main / res / layout / layout_1'
                'src / main / res / layout / layout_2',
                'src / main / res'
            ]
    }
}

第6步:总结:以上所有步骤将只帮助集群文件夹和显示在“项目”模式,而“android”模式将正常显示。 所以我认为命名前缀可能和集群文件夹一样有效。

我只是想补充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 $$$$

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

图片比文字更有价值。