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


当前回答

检查Bash Flatten Folder脚本,该脚本将文件夹层次结构转换为单个文件夹

其他回答

现在有了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.

你可以用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布局等)的直接父文件仍然需要与规范相对应。

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

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

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

我这样做的一种方式是在你的项目中创建一个与实际res文件夹相同级别的单独的res文件夹,然后你可以在你的应用程序build.gradle中使用这个

android {
    //other stuff

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

然后每个新res文件夹的子文件夹可以一些有关每个特定屏幕或一些在你的应用程序,和每个文件夹都有自己的布局/可拉的价值观等保持事物的组织和你不必须手动更新gradle文件和其他一些答案要求(同步你gradle每次添加一个新的资源文件夹,所以它知道它,并确保添加相关的子文件夹之前添加xml文件)。