问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/7653164/organizing-code-files-xml-files-for-android-sdk