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


当前回答

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

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

窗口:

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

Mac:

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

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

其他回答

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.

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

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

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

第一步:在资源管理器中右键单击布局-显示 步骤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”模式将正常显示。 所以我认为命名前缀可能和集群文件夹一样有效。

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

ln -s PATH_TO_YOUR_FILE

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

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

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