There are four types of identifier naming methods:
- Camel (Camel) nomenclature: Except for the first word, the first letter of all the remaining words is capitalized, such as: FooBar;
- Pascal nomenclature: The first letter of all words capitalized, such as: FooBar;
- Underline naming method: The word and the word with the underscore between the interval, such as: Foo_bar;
- Hungarian nomenclature: Widely used in Microsoft programming environment, in the Pascal nomenclature variable, the first letter lowercase description of the variable type. The number of names is: Scope_ prefix_qualifier range prefix, type prefix, qualifier, such as: G_foo_bar;
Android app Layer development is mainly the Java language, so the basic use in addition to the Hungarian nomenclature outside the naming method;
abbreviations are required in naming and follow the following rules:
- Shorter words can be abbreviated by removing "vowels", such as icon->ic;
- A longer word is preferable to the first few letters of a word to form abbreviations, such as:average->avg;
- In addition, there are some agreed idiomatic English word abbreviations, such as internationalization->i18n;
- Do not use abbreviations in the program, unless the abbreviation is customary.
Naming conventions:
- Package (packages): Use the anti-domain naming rules, all using lowercase letters. The first level of the package name is the top-level domain name, such as COM, two-level package named XX (can be a company or personal casually), three-level package name according to the application of the name, four-level package named Module name or hierarchy name; such as Com.tinyx.myapp.activities;
- Class (classes): with the Pascal nomenclature, as far as possible to avoid abbreviations, such as: myactivity; abbreviations are well known, such as Html,url; The class name contains a word abbreviation, each letter of the acronym should be capitalized, such as: publichtml, Commonurl.
- interface (interface): the same as the class with the Pascal nomenclature, more with able or ible end, more used as a representation of behavior, such as runnable,accessible;
Methods (methods): verbs or verb nouns, using the Camel name method, such as: OnCreate (), run (); Here are some suggestions:
- Initializes the associated method, using init as the prefix identifier, such as: Initialize layout initview ();
- Boolean uses is or check for prefix identification, such as: CheckValue (), isvalidate ();
- A method that returns a value, using get as the prefix identifier, such as: GetName ();
- Data for processing related, try to use process as the prefix identifier, such as: Processupdate ();
- Save data related, use Save as prefix identifier, such as: SaveData ();
- Resets the data, using the reset prefix identifier, such as: ResetData ();
- Clear the data correlation, using the clear prefix identifier, such as: ClearData ();
- Remove certain items, using the Remove prefix identifier, such as: RemoveItem ();
- Draw data or effects related, using the draw prefix identifier, such as: Drawcircle ();
- variable (variables): The use of Pascal Nomenclature, it is recommended to use a meaningful name such as: Firstname,lastname; The model class variable defaults to the above rule
Public class User { public String name; Public String phone; Public int // 1, male 2, female Public User () { this. Name = "MyName"; this. Phone = "123" This . sex = 0; }}
- The non-model class global parameters are suggested with a lowercase m opening;
Public class extends activity{ private zoomableimageview Mzoomableview; Private tablayout mtablayout; Private int Mitemscount; @Override protectedvoid onCreate (Bundle savedinstancestate) { Super . OnCreate (savedinstancestate); Setcontentview (r.layout.test_activity); }}
constant (constants): all uppercase, using the underscore name method as follows:
Public Static Final int max_items=; Public Static Final String TAG = User. class. Getsimplename ();
resource file naming (resources): using the underscore naming method, all lowercase, for different resources, the following naming method is recommended;
- drawable resource, prefix name: Prefix _ function _ module _ description. xml/png/
Description |
naming Examples |
Icon: suggested format ic_xxx; |
Ic_appicon.png |
Background: suggested format bg_xxx; |
Bg_normal_button_default.xml,bg_normal_button_press.xml |
- Layout resource file, prefix naming: Type _ Module _ function _ Description. XML, to cite some common examples are as follows:
Description |
naming Examples |
Activity layout File |
Activity_main.xml |
Fragment Layout file |
Fragment_main.xml |
Partial layout view File |
View_main_header.xml,view_main_bottom.xml |
Custom Prompt dialog box |
Dialog_alert.xml |
List items, etc. |
Fragment_user_list_item.xml |
- Animation Anim resource file (Anim has only one resource, so it doesn't have to be prefixed): Module _ function _ animation _ direction. Xml
Description |
naming Examples |
Fade |
Main_button_fade_in.xml |
Out |
Main_button_fade_out.xml |
Push in from below |
Button_push_down_in.xml |
Launched from below |
Main_button_push_down_out.xml |
- The menu resource file (menu has only one resource, so it does not have to be prefixed to differentiate), module _ function _ description. XML
Description |
naming Examples |
Main interface Menu |
Main_activity.xml |
Fragment Interface Menu |
User_fragment.xml |
- Values resources, which are mainly divided into the following resources:
1, IDS resources, the main storage is the ID value of the interface control, with the underscore lowercase naming method, prefix method: Prefix _ Module _ function _ Description, commonly used interface control named as follows:
Description |
naming Examples |
Layout and child controls (ViewGroup, custom view) |
View_main_topnav |
TextView |
Tv_main_title |
Button |
Btn_user_add |
ImageButton |
Imgbtn_user_del |
ImageView |
Img_thumb |
CheckBox |
Cb_sex |
RadioButton |
Rbtn_answer |
EditText |
Et_username |
ToggleButton |
Toggle_funtion |
ProgressBar |
Pb_download |
SeekBar |
Sb_progress |
ProgressBar |
Pb_download |
Videoview |
Vv_course |
WebView |
Wv_download |
Rantingbar |
Rb_download |
Spinner |
Sp_cities |
Scollview |
Sv_main |
Textswitch |
Sp_cities |
Listview/expandlistview/recyclerview |
Lv_cities |
Mapview |
Mv_location |
2, Strings/arrays/dimens resources, with the lower-case naming method, without any prefix, format, module _ function _ Description
3, the Attrs/colors/ids attribute and the name use the Camel name law; The Styles property uses the Camel nomenclature, which uses the Pascal name method;
<!--attrs--><attr name= "text" format= "string"/><attr name= "Itemicon" format= "Reference"/>< attr name= "Showtoggle" format= "boolean"/><attr name= "Showversion" format= "boolean"/><style name= " Theme.AppCompat.Light.NoActionBar "> <item name=" Windowactionbar ">false</item> <item name= "Windownotitle" >true</item></style> <!--colors--> <color name= " Colorprimary "> #009688 </color> <color name=" Colorprimarydark "> #00796b </color> <color name=" Coloraccent "> #cddc39 </color> <!--ids--> <item name=" tablayout "type=" id "/> <item name=" Viewpager "type=" id "/> <item name=" Viewcontainer "type=" id "/>
Have questions welcome message, write ([email protected]) or follow my public number:
Android you have to look at the naming conventions