组织code文件/ XML文件的Andr​​oid SDK文件、组织、code、XML

2023-09-06 09:50:08 作者:皆笑谈

有人可以提供一些策略来安排我的项目,以便它是干净的?说我有一大堆的活动;它的好处是把它们全部放入一个单独的包,而把其他类(如自定义适配器)的另一个包分离逻辑更好?

此外,对于布局创建XML文件时,我将如何分开的布局XML文件在逻辑上,如果我有一些布局,这对于自定义的行的某些活动和其他XML布局文件(带转接器来使用)我不T只想把他们都变成RES /布局 - 它会成为这样一个巨大的麻烦,当项目变得非常大。

解决方案   

说我有一大堆的活动;它的好处是把它们全部放入一个   单独的数据包,而把其他类(如自定义   适配器)在另一个包分离逻辑更好?

我不知道什么最好的做法,但这里就是我如何安排我的应用程序:我倾向于把我的活动 com.foo.appname.activity 内容在 com.foo.appname.service 提供商 com.foo.appname.content ,服务,并在通用公用事业 com.foo.appname.utils

对于像适配器辅助类,仅用于一个活动,我一般让他们静态内部类。如果他们在多个活动中使用的,我愿意给他们包级别的知名度,在活动套餐。

  

我不希望只是把他们全部纳入RES /布局

我不认为 RES 目录允许有子目录,所以最好你能做的就是拿出一个很好的命名方案。我通常preFIX同类型的布局文件: activity_foo.xml fragment_foo.xml

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.

解决方案

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.