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


当前回答

While all the proposals for multiple resource sets may work, the problem is that the current logic for the Android Studio Gradle plug-in will not update the resource files after they have changed for nested resource sets. The current implementation attempts to check the resource directories using startsWith(), so a directory structure that is nested (i.e. src/main/res/layout/layouts and src/main/res/layout/layouts_category2) will choose src/main/res/layout/layouts consistently and never actually update the changes. The end result is that you have to rebuild/clean the project each time.

我在https://android-review.googlesource.com/#/c/157971/上提交了一个补丁,试图帮助解决问题。

其他回答

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

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

buying_bought_tracks.xml
buying_buy_tracks.xml
playing_edit_playlist.xml
playing_play_playlist.xml
playing_show_playlists.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.

现在我们可以很容易地使用JetBrains插件“Android文件分组”

看看这个链接

Android文件分组

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