Android coding specification, android Encoding
It was very difficult to find a job during my new internship. Basically, my new resume was filled with great difficulties, because I didn't have practical project development experience, a great background, and more importantly, I didn't really learn it, even though I studied computer science during my college years, I was also a class student, ...... You know.
Later, a company barely took me in, and a problem immediately appeared in front of me, that is, the variable name during code writing: TextView, ListView ...... Almost every. java or. xml file has different naming formats. In fact, I felt speechless when reading my own code.
Later, I wanted to unify my naming method. After trying for a while, I felt like this was quite good. Yes, there should be a uniform naming method, which makes it easier to write and read code.
The following are my naming conventions (I have found them on the Internet, but I cannot remember the source). In fact, no matter what naming conventions are used, the ultimate goal is the same-writing excellent code, at least making yourself and others not very difficult to read.
I. Android coding specifications
1. the java code does not contain Chinese characters. A maximum of Chinese characters can appear in comments.
2. Local variable naming and static member variable naming
It can only contain letters. The first letter of a word must be in upper case and the other letters must be in lower case.
3. Constant naming
Only letters and, All uppercase letters, used between wordsSeparate
4. Name the id in layout
Naming mode: view abbreviation _ module name_view logical name
The abbreviated view details are as follows:
LayoutView: lv RelativeView: rv TextView: TV ImageView: iv ImageButton: im Button: btn
The abbreviation of layout is as follows:
FrameLayout: fraLay RelativeLayout: relLay TextView: TV ImageView: iv ImageButton: im Button: btn
5. Name the view variable in the activity
Naming mode: Logical name + view abbreviation
Suggestion: If the layout file is complex, we recommend that you divide layout into multiple modules. Each module defines a moduleViewHolder whose member variables contain the view
6. id naming in strings. xml
Naming mode: activity nameFunction module nameLogical name/activity nameLogical name/commonLogical name
In strings. xml, use activity name annotations to differentiate the file content.
Name the image in drawable
Naming mode: activity nameLogical name/commonLogical name
7. styles. xml: extract the styles that are repeatedly reproduced in layout into general style components and put them in styles. xml;
8. Use layer-list and selector
9. Split images into multiple reusable images as much as possible
10. What the server can achieve is not to be placed on the client.
11. Exercise caution when referencing a third-party library. Avoid using a large third-party library, resulting in a large client package.
12. Handle global application exceptions and errors and send the errors to the server by email.
13. Image Processing
14. Exercise caution when using static variables to share Interfaces
15. Log (system name module Name Interface Name, detailed description)
16. unit test (logical test and Interface Test)
17. Do not reuse the handler of the parent class. The handler of the corresponding class should not be used by its subclass. Otherwise, the message. what conflict will occur.
18. process all logic in a View. OnClickListener in the activity
19. Use % 1 $ s in strings. xml for character string wildcard
20. If multiple activities contain common UI processing, you can extract a CommonActivity and call the common part for processing. Other activities only need to inherit it.
21. When you use button + activitgroup to implement the tab effect, use Button. setSelected (true) to make sure that the button is in the selected State and that the current activity of activitygroup corresponds to the button.
22. If a common component is developed, prefix the file name in the drawable/layout/menu/values directory to avoid conflicts.
23. data must be validated, such
The numeric type must have a default value if the conversion fails;
Determine whether the response data of the Android development server is valid;
Ii. Android development performance optimization
1. Use gzip for http compression and set the connection timeout and response timeout.
Http requests can be divided into cache and non-cache based on business needs. In a network-less environment, some data is still browsed through the cached httpresponse for offline reading.
2. listview Performance Optimization
(1) Reuse convertView
In getItemView, determine whether convertView is empty. If it is not empty, it can be reused. If you need to add listerner to the view in couvertview, the Code must be out of if (convertView = null.
(2) asynchronously loading Images
If the item contains a webimage, it is best to load it asynchronously.
(3) The image is not displayed during fast sliding.
When the list is quickly swiped (SCROLL_STATE_FLING), the image in the item or the view that consumes resources can be retrieved without being displayed. The image is in the other two States (SCROLL_STATE_IDLE and SCROLL_STATE_TOUCH_SCROLL ), the view is displayed.
3. Use a thread pool, which can be divided into a core thread pool and a common thread pool. Download images and other time-consuming tasks are placed in the common thread pool to prevent time-consuming tasks from blocking the thread pool, as a result, all asynchronous tasks must wait.
4. asynchronous tasks are divided into core tasks and common tasks. Only system-level errors in core tasks report errors. The ui operation of asynchronous tasks needs to determine whether the original activity is active.
5. Avoid using static member variables to reference instances that consume too much resources, such as Context
6. Use WeakReference to replace strong references. Weak references can be used to keep references to objects. GC is also allowed to release objects and reclaim memory when necessary. For those objects that create cheap but consume a large amount of memory, that is, you want to keep the object and use it when the application needs it. If you want GC to recycle it when necessary, you can consider using weak references.
7. Super fat Bitmap
Timely destruction (bitmap is recycled when the onDestroy of the Activity is used by the UI component. RuntimeException: Canvas: tryingtousearecycledbitmapandroid is thrown when it is recycled immediately after it is used by the UI component. graphics. bitmap) sets a certain sampling rate (sampling is not required for images provided by developers. For images uploaded by users or uncontrolled by third parties, sampling can be performed to reduce the memory occupied by images ), when returning images from the server, we recommend that you use soft references to the resid resources of the drawable image. bitmap corresponds to any type of images of other resources. If it cannot be obtained (for example, the file does not exist, or when an OutOfMemory exception is run during file reading, there should be a corresponding default image (the default image is placed in the apk and obtained through resid );
8. Ensure that the memory occupied by Cursor is promptly released, rather than waiting for GC to process. In addition, Android obviously prefers programmers to manually close Cursorclose.
9. threads are also an important source of Memory leakage. The main cause of thread Memory leakage is the uncontrollable thread lifecycle.
10. If the ImageView image is from the network, load it asynchronously.
11. During custom views in application development, do not write the interaction part as a thread to constantly refresh the interface display. Instead, update the interface based on the TouchListener event.
12. Drawable
The image used by the ui component is included in the apk package. Therefore, setImageResource or setBackgroundResource is used instead of resourceid.
Note: get (getResources (), R. drawable. btn_achievement_normal) This method is converted to drawable through resid. You need to consider the recycling problem. If drawable is a private object of the object, the memory will not be released before the object is destroyed.
3. UI Optimization for Android Development
1. layout componentization, use merge and include reuse whenever possible
2. Use styles to reuse style definitions
3. the pop-up control of the soft keyboard should not overwrite the input box
4. Placement of numbers, letters, and Chinese characters: All numbers and letters are divided into different types. In most cases, because our input is half-width, the placeholder values of letters and numbers cannot be determined. However, once fully divided, the placeholder values of numbers and letters are the same as those of a Chinese character, in this way, the layout problem caused by placeholder can be avoided.
5. English Document Layout: When textview is automatically changed, the word integrity should be maintained. The solution is to calculate the string length, and then manually set the number of letters displayed in each row with '\ n' added'
6. Use RelativeLayout for complex la s
7. adaptive screen; Use dp to replace pix
8. Use android: layout_weight or TableLayout to create an equals layout.
9. Use animation-list to create an animation.
Code element variable name: