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


当前回答

顶部答案有几个缺点:你必须添加新的布局路径,AS将新的资源放在res\layouts文件夹而不是res\values。

结合几个答案,我写下了类似的内容:

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

我用这篇文章做了文件夹:http://alexzh.com/tutorials/how-to-store-layouts-in-different-folders-in-android-project/。为了创建子文件夹,你应该使用这个菜单:新建>文件夹> Res文件夹。

更新

几周后,我发现Android Studio并没有注意到资源的变化。于是,一些奇怪的虫子出现了。例如,布局仍然显示旧的尺寸和页边距。有时AS找不到新的xml文件(特别是在运行时)。有时它混合了视图id(对另一个xml文件的引用)。通常需要按Build > Clean Project或Build > Rebuild Project。在Android Studio中更改xml布局文件后,阅读需要重新构建的文件。

其他回答

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

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

配置如下:

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

检查文档。

答案是否定的。

我想提请您注意这本书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.

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

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

图片比文字更有价值。

在一个模块中,要拥有风味、风味资源(布局、值)和风味资源的组合,主要要记住两件事:

When adding resource directories in res.srcDirs for flavor, keep in mind that in other modules and even in src/main/res of the same module, resource directories are also added. Hence, the importance of using an add-on assignment (+=) so as not to overwrite all existing resources with the new assignment. The path that is declared as an element of the array is the one that contains the resource types, that is, the resource types are all the subdirectories that a res folder contains normally such as color, drawable, layout, values, etc. The name of the res folder can be changed.

一个例子是使用路径"src/flavor/res/values/strings-ES",但是注意到实践层次结构必须有子目录值:

├── module 
   ├── flavor
      ├── res
         ├── values
            ├── strings-ES
               ├── values
                  ├── strings.xml
               ├── strings.xml
 

该框架精确地根据类型识别资源,这就是为什么通常已知的子目录不能被省略。

还要记住,flavor中的所有strings.xml文件将形成一个联合,这样资源就不能被复制。反过来,这个组成该类型文件的联合在模块的主文件之前具有更高的优先级。

flavor {
        res.srcDirs += [
            "src/flavor/res/values/strings-ES"
        ]
}

将strings-ES目录视为包含资源类型的定制资源。

GL

顶部答案有几个缺点:你必须添加新的布局路径,AS将新的资源放在res\layouts文件夹而不是res\values。

结合几个答案,我写下了类似的内容:

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

我用这篇文章做了文件夹:http://alexzh.com/tutorials/how-to-store-layouts-in-different-folders-in-android-project/。为了创建子文件夹,你应该使用这个菜单:新建>文件夹> Res文件夹。

更新

几周后,我发现Android Studio并没有注意到资源的变化。于是,一些奇怪的虫子出现了。例如,布局仍然显示旧的尺寸和页边距。有时AS找不到新的xml文件(特别是在运行时)。有时它混合了视图id(对另一个xml文件的引用)。通常需要按Build > Clean Project或Build > Rebuild Project。在Android Studio中更改xml布局文件后,阅读需要重新构建的文件。