Android-code-style1. Conventions
- Activity.oncreate (), fragment.onactivitycreated (), immediately following the member variable, keep the method inside simple and try to call only the Initxxx () method, such as: InitData (), Initview ()
- The calling method keeps the "proximity principle", the called method, placed below the calling method
- Single method body not too long
- Don't misspell words anywhere in the code
Unified adjustment of the IDE's tab indent to 4 spaces
2. Name 2.1. Name of the ID in the layout fileRule: Use hump naming, prefix + logical name, class variable name and layout file ID name to be consistent, no underscore split
Control |
abbreviation prefix |
Textview/edittext |
Text |
Button/radiobutton/imagebutton |
Btn |
Relativelayout/linearlayout/framelayout |
Layout |
Listview |
Listview |
WebView |
WebView |
CheckBox |
CheckBox |
ProgressBar |
ProgressBar |
SeekBar |
SeekBar |
Other controls |
Control name initials as prefix |
- such as: TextView @+id/texttitle
- such as: EditView @+id/textname
such as: Button @+id/btnsearch
2.2. layout file namingRule: Use the prefix _ logical name, the words are all lowercase, the words are divided by an underscore.
Layout Type |
Layout prefix |
Activity |
Activity_ |
Fragment |
Fragment_ |
Include |
Include_ |
Dialog |
Dialog_ |
Popupwindow |
Popup_ |
Menu |
Menu_ |
Adapter |
Layout_item_ |
2.3. resource file naming
Rule: Use prefix _ Use name, the words all lowercase, the words are divided by an underscore.
- Picture resource file naming
prefix |
Description |
Bg_xxx |
Various background images |
Btn_xxx |
This button has no other state |
Ic_xxxx |
Icons, typically used for individual icons |
Bg_ Description _ Status 1[_ status 2] |
For different states on a control |
Btn_ Description _ Status 1[_ status 2] |
For different states on a button |
Chx_ Description _ Status 1[_ status 2] |
Selection box, typically 2 states and 4 states |
- Third-party resource files, regardless of the value, drawable
must carry a third-party resource prefix |
|
Umeng_socialize_style.xml |
|
Pull_refresh_attrs.xml |
|
2.4. Class and interface naming
Rule: Using hump rules, the first letter must be capitalized, using nouns or noun phrases. The requirements are straightforward, descriptive, and do not allow for meaningless or erroneous words.
class |
Description |
For example |
Application class |
Application for suffix identification |
Xxxapplication |
Activity class |
Activity is the suffix identifier |
Splash screen Page class splashactivity |
Parsing classes |
handler for suffix identification |
|
Public method Classes |
Utils or manager for suffix identification |
Thread Pool Management Classes |
Threadpoolmanager |
Log Tool class |
Logutils |
Database classes |
Identified by DBHelper suffix |
Mysqlitedbhelper |
Service class |
Identified as a service suffix |
Playback service: Playservice |
Broadcastreceiver class |
The suffix is identified by broadcast |
Time Notice: timebroadcast |
ContentProvider class |
The suffix is identified by provider |
Word content provider Dictprovider |
Shared base class for direct write |
Prefixed with base |
Baseactivity,basefragment |
2.5. Naming the method
Rule: Using hump rules, the first letter must be lowercase, using verbs. The requirements are straightforward, descriptive, and do not allow for meaningless or erroneous words.
Method |
Description |
INITXX () |
Initialize the associated method, using init as the prefix identifier, such as Initialize Layout Initview () |
Httpxx () |
HTTP business request method, identified by HTTP prefix |
GETXX () |
A method that returns a value that is identified with a get prefix |
Savexx () |
Associated with saving data, use Save as the prefix to identify |
Deletexx () |
Delete operation |
RESETXX () |
For data reorganization, use the reset prefix to identify |
Clearxx () |
Clear data-related |
ISXX () |
Method returns a Boolean value using is or check for prefix identification |
PROCESSXX () |
Methods of processing data, using process as a prefix to identify |
Displayxx () |
Pop-up tips and hints, using display as a prefix to identify |
Drawxxx () |
Drawing data or effects related, using the draw prefix to identify |
2.6. Variable naming
Rule: Using hump rules, the first letter must be lowercase, using nouns or noun phrases. The requirements are straightforward, descriptive, and do not allow for meaningless or erroneous words.
- Split the bloated method, one thing for each method
- Do the same logical approach, as close as possible to a piece, for easy viewing
- Do not use try catch to process business logic
- Do not manually parse and assemble data using the JSON tool class
- Reduce conditional nesting, no more than 3 layers
- If Judge uses "Guardian statement", reduce the level
if (obj! = null) {dosomething ();}
Modified to:
if (obj = = null) {return;} dosomething ();
- The IF statement must be included with {}, even if there is only one sentence.
- Dealing with mysterious numbers like "magic number" and so on.
- Don't show numbers in your code, especially some that identify different types of numbers.
- All meaning figures are extracted into the constant public class, and are not scattered among the classes.
- Blank line: Empty lines are separated by logical related code snippets, concise and clear, improve readability
- Between member variables, grouped by business formation, plus empty lines
- Add blank lines between methods
- Record ideas, record function points, use Todo to record temporary ideas during development, or leave a perfect explanation for not disturbing ideas
- Delete useless todo, development tool automatically generated TODO, or already perfect todo, be sure to delete.
Encoding Specifications for Android