创建资源结构的最佳实践
我的项目的 res/layout* 目录最近变得非常混乱,因为据我所知,不允许有子目录来改变它。所以很难在那里建立结构——你们是怎么做到的?
the res/layout* dir of my projects are getting really messy lately and as there are afaik no subdirs allowed there to change that. So it is hard to get structure in there - how do you guys do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不了解其他人,但就我自己而言,我喜欢使用前缀来帮助排序,本质上是代替子目录。例如,我所有的活动布局都以“activity_”开头,所有 UI 控件都以“control_”开头,通知以“notification_”开头,等等。
然后我留下一个至少具有某种结构的平面目录,例如:
activity_graph。 xml
Activity_main.xml
Activity_map.xml
control_graph.xml
control_title_bar.xml
notification_just_label.xml
notification_with_progress_bar.xml
...
这并不完美,但它对我有用。
I don't know about others but for myself I like to use prefixes to help keep things sorted, essentially taking the place of subdirectories. For example, all my activity layouts start with 'activity_' and all my UI controls start with 'control_', notifications with 'notification_', etc.
I am then left with a flat directory with at least some structure, something like:
activity_graph.xml
activity_main.xml
activity_map.xml
control_graph.xml
control_title_bar.xml
notification_just_label.xml
notification_with_progress_bar.xml
...
It's not perfect, but it works for me.
抱歉,除了文档规范之外,布局文件夹内不可能有子文件夹。
只需检查此和此和这个问题。
Sorry subfolder inside the layout folder is not possible other than docs specification.
Just check this and this and this question.
另一种可能性是将应用程序的某些元素重构到库中。
例如,您可以将不相关的片段(因为片段本身应该不相关)提取到库中,将该项目标记为 Android 库(在项目属性中检查“是库”),然后在主项目中使用您自己的库。
单独的库中所需的资源将不再发生冲突,但会在构建时“扔在一起”,因此访问没有问题。
以下是相关的文档。
Another possibility is to refactor some elements of your application into libraries.
For example you could extract unrelated fragments (as fragments should per se be unrelated) into libraries, mark that project as an Android library (check "Is library" in project properties) and then use your own libraries in your main project.
The resources you need within the separate libs will not collide anymore, but will be "thrown together" at build time, so access is fine.
Here are the docs for that.