组织Android SDK的代码文件/XML文件
有人可以提供一些策略来组织我的项目以使其干净吗?假设我有很多活动;将它们全部放入一个单独的包中,同时将其他类(例如自定义适配器)放入另一个包中以更好地分离“逻辑”是否很好?
另外,在为布局创建 XML 文件时,如果我有一些用于某些活动的布局和用于自定义“行”(与适配器一起使用)的其他 XML 布局文件(与适配器一起使用),我将如何在逻辑上分离布局 XML 文件把它们全部扔进 res/layout 中——当项目变得非常大时,这会变得非常麻烦。
Can someone provide some strategies as to organizing my project so that it is clean? Say I have a bunch of activities; is it good to place them all into a separate package, while putting other classes (such as custom Adapters) in another package to separate "logic" better?
Also, when creating XML files for layouts, how would I separate the layout XML files logically if I have some layouts that are for certain activities and other XML layout files for custom "rows" (to use with an Adapter) I don't want to just throw them all into res/layout -- it would become such a huge hassle when the project gets really big.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定最佳实践是什么,但我是这样组织应用程序的:我倾向于将活动放在
com.foo.appname.activity
中,将内容提供程序放在com.foo 中。 appname.content
、com.foo.appname.service
中的服务以及com.foo.appname.utils
中的通用实用程序。对于像适配器这样仅由一个活动使用的帮助器类,我通常将它们设置为静态内部类。如果它们在多个活动中使用,我会在活动包中为它们提供包级别的可见性。
我认为
res
目录不允许有子目录,所以你能做的最好的就是想出一个好的命名方案。我通常在布局文件中添加以下类型的前缀:activity_foo.xml
、fragment_foo.xml
等。I'm not sure what best practices are, but here's how I organize my applications: I tend to put my activities in
com.foo.appname.activity
, content providers incom.foo.appname.content
, services incom.foo.appname.service
, and generic utilities incom.foo.appname.utils
.For helper classes like
Adapters
that are only used by one activity, I typically make them static inner classes. If they're used in multiple activities, I'd give them package level visibility in the activity package.I don't think that the
res
directories are allowed to have subdirectories, so the best you can do is to come up with a good naming scheme. I typically prefix the layout file with the type:activity_foo.xml
,fragment_foo.xml
, etc.当然,所有这些建议都是您的选择。但是当我开发某些东西时,我习惯将逻辑层与“可见”层和类分开。我的意思是我使用不同的包,因为
我还为所有这些包创建了不同的包,这样您就可以更好地组织它们。但这始终是您的选择。
还有你的布局...我不知道你是否可以更好地组织布局。当您生成项目时,如果您看到,在您的 gen 文件夹中有 R.java 类。该类自动检测文件夹,例如布局、可绘制、原始...但我不确定您是否可以在其中创建子文件夹。
All these suggestions, of course are your choice. But when I develop something, I use to separate logical layer from "visible" layers and classes. I mean that I use different packages for
I also create different packages for all of them, so you can organize them better. But this is always your choice.
And with your layout... I don't know if you can organize better the layout. When you generate your project, if you see, in your gen folder there's the R.java class. That class auto-detects folders such as layout, drawable, raw... But I'm not sure if you can make subfolders inside it.