Android Encoding Specification

Source: Internet
Author: User

Just internship to find a job when it is very difficult, basically cast out resumes are not, because there is no actual project development experience, no good background, more important is no real knowledge, although I am in college reading is a computer professional, also is trained, but ... You know.

Then reluctantly a company took me in, then a question immediately appeared in front of me, that is to write code when the name of the variable: TextView, ListView ... Almost every one. The format that is named in the Java or. xml file is different. Actually, I also feel very silent when I read the code I write.

Later, I was thinking of unifying the way I named it, and after a while it felt like it was pretty good. Yes, there should be a unified naming method, which makes it easier to write and read code.

The following are the naming habits I used (in fact, it is also found on the Internet, but I have not been aware of the source), in fact, no matter what the name of the way, the ultimate goal is the same-to write excellent, at least let oneself, others read not very laborious code.

One, the Android development coding specification

1, the Java code does not appear in Chinese, the most comments can appear in Chinese

2, local variable name, static member variable name

Can only contain letters, the first letter of a word, all uppercase, the other letters are lowercase

3, the constant name

Can only contain letters and , all uppercase letters, separated by words

4. Name of ID in layout

Named mode: The logical name of the view abbreviation _ Module name _view

The details of view abbreviations are as follows

LAYOUTVIEW:LV relativeview:rv textview:tv imageview:iv imagebutton:im button:btn

The abbreviated details of layout are as follows

Framelayout:fralay relativelayout:rellay textview:tv imageview:iv imagebutton:im Button:btn

5. Name of view variable in activity

Named mode: logical name +view abbreviation

Recommendation: If the layout file is complex, it is recommended that layout be divided into modules, each of which defines a moduleviewholder whose member variables contain the view

6. Name of ID in Strings.xml

Naming pattern: Activity name function module name logical name/activity name logical name/common logical Name

Strings.xml, use activity name comments to separate the contents of the file

Picture naming in Drawable

Naming pattern: Activity name logical name/common logical Name

7, Styles.xml: The layout of the constantly reproduced style to extract the general style universal components, put into the styles.xml;

8. Using Layer-list and Selector

9, the picture as far as possible to split into multiple reusable pictures

10, the server can be implemented, do not put on the client

11, reference third-party library to be cautious, avoid the application of large-capacity third-party libraries, resulting in a very large client package

12, handle the application of global exceptions and errors, the error is sent to the server in the form of mail

13, the image processing

14, using static variable method to realize the sharing between the interface to be cautious

15, Log (System name Module name Interface name, detailed description)

16. Unit Test (logic test, Interface test)

17, do not reuse the parent class handler, corresponding to a class of handler should not let its subclasses use, otherwise it will cause message.what conflict

18, activity in a view.onclicklistener to deal with all the logic

19. Using%1 $ s in Strings.xml to implement string wildcard

20, if more than one activity contains a common UI processing, then you can refine a commonactivity, the common part is called by it to handle, other activity as long as inherit it

21. When using Button+activitgroup to implement the tab effect, use button.setselected (true) to ensure that the button is selected and that the current activity of activitygroup corresponds

22. If you are developing a generic component, to avoid conflicts, add a prefix to the file name in the Drawable/layout/menu/values directory

23, the data must be tested, for example

The character type turns the numeral type, if the conversion fails must have the default value;

Android Development Server response data is valid judgment;

Second, Android development performance optimization

1, HTTP with gzip compression, set the connection time-out and response time-out

The HTTP request is divided into whether it can be cached and not cacheable according to the business demand, then, in the network-free environment, the cached HttpResponse is still used to browse some data and realize offline reading.

2. ListView Performance Optimization

(1) Multiplexing Convertview

In Getitemview, determine if the Convertview is empty, and if it is not empty, it can be reused. If the view in Couvertview needs to add Listerner, the code must be outside the if (Convertview==null) {}.

(2) Loading images asynchronously

If the item contains Webimage, it is best to load asynchronously

(3) Do not display pictures when sliding quickly

When a quick slide list (scroll_state_fling), the picture in item or the view that needs to consume resources, can not be displayed, but in the other two states (Scroll_state_idle and Scroll_state_touch_ SCROLL), the view is displayed

3, the use of thread pool, divided into the core thread pool and common thread pool, download pictures and other time-consuming tasks placed in the normal thread pool, to avoid time-consuming tasks blocking the thread pool, resulting in all asynchronous tasks must wait

4, asynchronous tasks, divided into core tasks and common tasks, only the core tasks in the system-level errors will be error, the UI operation of the asynchronous task needs to determine whether the original activity is in the active state

5, try to avoid the static member variable reference resources too expensive instances, such as context

6. Using WeakReference instead of strong references, weak references can keep references to objects while allowing the GC to dispose of objects and reclaim memory when necessary. Consider using weak references for those objects that create inexpensive but expensive memory, want to keep the object, and use it when the application needs it, while you want the GC to be recycled as necessary.

7, Super Big Fat bitmap

Timely destruction (when the activity is OnDestroy, bitmap is recycled and will be discarded immediately after being used by the UI component Runtimeexception:canvas: TRYINGTOUSEARECYCLEDBITMAPANDROID.GRAPHICS.BITMAP) Set a certain sample rate (there is no need for the developer to sample the image, for users to upload or third-party size of the uncontrolled picture, can be sampled to reduce the memory of the picture) , returning pictures from the server, suggesting that the size of the feedback picture cleverly apply the soft reference drawable corresponding to Resid resources, bitmap corresponding to other resources of any type of picture, if not obtained (for example, the file does not exist, or read the file run OutOfMemory exception), There should be a corresponding default picture (the default image is placed in the APK, obtained by RESID);

8. Ensure that the memory that the cursor occupies is released in a timely manner, rather than waiting for the GC to handle it. And Android obviously is prone to programmers manually will cursorclose off

9. Threads are also an important source of memory leaks. The main reason for a thread's memory leak is that the thread life cycle is not controllable

10, if the picture of ImageView is from the network, asynchronous loading

11, the application development in the custom view, the interactive part, do not write thread constantly refresh the interface display, but according to the Touchlistener event active trigger interface update

12, drawable

The image that the UI component needs to use is the APK package, so use Setimageresource or Setbackgroundresource instead of the ResourceID

Note: Get (Getresources (), R.drawable.btn_achievement_normal) This method is converted to drawable by RESID, you need to consider the issue of recycling, if drawable is an object private object, The memory is definitely not freed until the object is destroyed.

Third, Android Development UI optimization

1, layout components, as far as possible to use the merge and include reuse

2, using styles, reuse style definition

3, soft keyboard pop-up control, do not let it overwrite the input box

4, numbers, letters and Chinese characters mixed position problem: The number and letter full-angle. Since most of the time our input is half-width, the placeholder for letters and numbers cannot be determined, but once full-width, the digits and the letters occupy the same position as a Chinese character, which avoids the typographical problems caused by the occupying position.

5, English document typesetting: TextView to maintain the integrity of the word, the solution is to calculate the length of the string, and then manually set how many letters each line displays and add ' \ n '

6. Complex layout using Relativelayout

7. Adaptive screen, using DP instead of PIX

8, using Android:layout_weight or tablelayout to make sub-layout

9. Use Animation-list to make animation effect
code element variable name:

Android Encoding Specification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.