组织Android SDK的代码文件/XML文件

发布于 2024-12-08 10:50:18 字数 237 浏览 1 评论 0原文

有人可以提供一些策略来组织我的项目以使其干净吗?假设我有很多活动;将它们全部放入一个单独的包中,同时将其他类(例如自定义适配器)放入另一个包中以更好地分离“逻辑”是否很好?

另外,在为布局创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三岁铭 2024-12-15 10:50:18

假设我有很多活动;是否可以将它们全部放入一个
单独的包,同时放置其他类(例如自定义
适配器)在另一个包中更好地分离“逻辑”?

我不确定最佳实践是什么,但我是这样组织应用程序的:我倾向于将活动放在 com.foo.appname.activity 中,将内容提供程序放在 com.foo 中。 appname.contentcom.foo.appname.service 中的服务以及 com.foo.appname.utils 中的通用实用程序。

对于像适配器这样仅由一个活动使用的帮助器类,我通常将它们设置为静态内部类。如果它们在多个活动中使用,我会在活动包中为它们提供包级别的可见性。

我不想把它们全部扔进 res/layout

我认为 res 目录不允许有子目录,所以你能做的最好的就是想出一个好的命名方案。我通常在布局文件中添加以下类型的前缀:activity_foo.xmlfragment_foo.xml等。

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?

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 in com.foo.appname.content, services in com.foo.appname.service, and generic utilities in com.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 want to just throw them all into res/layout

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.

人疚 2024-12-15 10:50:18

当然,所有这些建议都是您的选择。但是当我开发某些东西时,我习惯将逻辑层与“可见”层和类分开。我的意思是我使用不同的包,因为

a) Activites
b) Classes or Objects
c) Interface classes
d) Database classes
e) Interaction with Database

我还为所有这些包创建了不同的包,这样您就可以更好地组织它们。但这始终是您的选择。

还有你的布局...我不知道你是否可以更好地组织布局。当您生成项目时,如果您看到,在您的 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

a) Activites
b) Classes or Objects
c) Interface classes
d) Database classes
e) Interaction with Database

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文