Android encoding standard

Source: Internet
Author: User

Just practice is very difficult to find a job at that time, the resume is basically out of the way, because there is no practical experience in project development, no cool background, more importantly, no real talent, although I was in college reading computer science, it can be considered a technical education. But...... You know.

Then reluctantly a company took me in, then a question immediately out of my face. That is the name of the variable when you write the code: TextView. Listview...... Almost every. java or. xml file is named in a different format. In fact, I also feel very silent when I read the code I wrote.

Then I was thinking about unifying the way I named myself. After trying for a while it feels like it's pretty good. Yes, there should be a unified naming method, which makes it easier to write and read code.

Here are the habits I used to name (in fact, I found it on the internet, but I can't remember the source). In fact, no matter what the naming method, the ultimate goal is the same-to write good, at the very least, let yourself, others read not very laborious code.

One, the Android development coding specification

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

2, local variable name, static member variable name

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

3, the constant name

Can only include 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 abbreviation details for the view are as follows

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

The abbreviated details of layout such as the following

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: Assuming the layout file is very complex, it is recommended that layout be divided into modules, each defining a moduleviewholder. Its member variables include the owning 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 the activity name to stare. Separating the contents of a 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 refine the common style generic components. Put it in 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 achieved, do not put in 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, the use of static variables to realize the sharing between the interface to be cautious

15, Log (System name Module name Interface name, specific description of the narrative)

16. Unit Test (logic test, Interface test)

17, do not reuse the handler of the parent class, the corresponding class of handler should not let its subclasses use, otherwise it will lead to 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. Suppose that multiple activity includes common UI processing. It is then possible to refine a commonactivity and call the generic part to handle it, and the other activity simply inherits 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

22, assume that the development of the common components, in order to avoid conflicts, the Drawable/layout/menu/values folder under the file name to add a prefix

23, the data must be tested, such as

Character-to-digital type, assuming that the conversion failed must have a default value.

Android Development Server response data is valid inference;

Second, Android development performance optimization

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

HTTP requests follow business requirements. Can be divided into cache and non-cacheable, in a network-free environment. Part of the data is still browsed through the cached HttpResponse for offline reading.

2. ListView Performance Optimization

(1) Multiplexing Convertview

In the Getitemview. Infers whether the Convertview is empty, assuming that it is not empty. Available for reuse.

Assuming that the view in Couvertview needs to be added to the Listerner, the code must be outside the if (Convertview==null) {}.

(2) Loading images asynchronously

The item is assumed to include Webimage. Then it's best to load asynchronously

(3) Do not display pictures when sliding at high speed

When the high-speed sliding list (scroll_state_fling). The picture in item or the view that requires resource consumption 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, avoid time-consuming tasks blocked thread pool, resulting in all asynchronous tasks must wait

4, asynchronous tasks, divided into the core tasks and common tasks, only the core tasks in the system-level errors will be error. The UI action of an asynchronous task needs to infer whether the original activity is active or inactive

5, as far as possible to avoid static member variable reference resources too expensive instances, such as context

6. Use WeakReference instead of strong references. A weak reference keeps a reference to the object and at the same time agrees to the GC releasing the object if necessary. Reclaim memory. For those objects that create a cheap but expensive memory, you want to keep the object. When the application needs to be used, the same time you want the GC to be recycled when necessary, you can consider using weak references.

7, Super Big Fat bitmap

Timely destruction (OnDestroy of the activity will be bitmap recycled and will be thrown runtimeexception:canvas if it is recycled immediately after being used by the UI component: TRYINGTOUSEARECYCLEDBITMAPANDROID.GRAPHICS.BITMAP) Set a certain sample rate (there is no need to sample the images provided by the developer. For real user uploads or third-party size uncontrolled images. Can be taken to reduce the image of the memory), from the server to return pictures. Suggest the same time to feedback the size of the picture clever use of soft reference drawable the corresponding Resid resources, bitmap the corresponding other resources regardless of what kind of picture, assuming not get (such as 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 via 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 is obviously prone to programmers who manually cursorclose out

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, assume that the ImageView picture is from the network. To load asynchronously

11, the application development of their own definition of the view when the interactive part. Do not write threads constantly refresh the interface display, but based on the Touchlistener event active trigger interface update

12, drawable

The image that the UI component needs to use is the APK package itself. Then always use Setimageresource or setbackgroundresource, not according to ResourceID

Note: Get (Getresources (), R.drawable.btn_achievement_normal) This method is converted to drawable by Resid, and the issue of recycling needs to be considered. Assuming that drawable is an object private object, it is certainly not free of memory until the object is destroyed.

Third, Android Development UI optimization

1, layout of the component. Use merge and include reuse as much as possible

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. Because in most cases our input is half-width. So the placeholder for letters and numbers cannot be determined, but once full-width, the numbers and 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 when you change the line, the solution is to calculate the length of the string, and then manually set how many letters each row 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:

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Android encoding standard

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.