The naming rules for Android development that you must not read
There are four types of identifiers:
- Camel (Camel) naming method: except the first word, the first letter of all other words is capitalized, such as: fooBar;
- Pascal naming: the first letter of all words in uppercase, for example, FooBar;
- Underline naming: use an underscore to separate words, for example, foo_bar;
- Hungary name method: This method is widely used in Microsoft programming environments. For a variable using the Pascal name method, lowercase letters indicate the type of the variable. The name of the quantity is: scope _ prefix_qualifier range prefix, type prefix, qualified word, such as: g_foo_bar;
Android App layer development is mainly based on the Java language, so the naming method except the Hungarian naming method is basically used;
The abbreviation is mandatory and follows the following rules:
- Short words can be abbreviated by removing the "vowel", such as icon-> ic;
- Long words can be abbreviated to the first few letters of a word, for example, average-> avg;
- In addition, there are some common English word abbreviations, such as Internationalization-> I18N;
- Do not use abbreviations in programs unless they are conventions.
Naming rules:
Public class User {public String name; public String phone; public int sex; // 1, male 2, female public User () {this. name = "myname"; this. phone = "123" this. sex = 0 ;}}
- For non-model Global parameters, we recommend that you start with lowercase m;
public class TestActivity extends Activity{ private ZoomableImageView mZoomableView; private TabLayout mTabLayout; private int mItemsCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_activity); }}
Constant (constants ):All are capitalized and the underline naming method is used as follows:
public static final int MAX_ITEMS= 10;public static final String TAG = User.class.getSimpleName();
Resource file name (resources ):We recommend that you use the following naming method for different resources by using the underline naming method in lower case;
- Drawable resource, prefix Name:Prefix _ function _ module _ description. xml/png/
| Description |
Naming example |
| Icon: the recommended format is ic_xxx; |
Ic_appicon.png |
| Background: the recommended format is bg_xxx; |
Bg_normal_button_default.xml, bg_normal_button_press.xml |
- Layout resource file, prefix name: type _Module _ function _ description. xml,Some common examples are as follows:
| Description |
Naming example |
| 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 |
Fragment_user_list_item.xml |
- Animated anim resource file (anim only has one resource, so you do not need to add a prefix to differentiate it ):Module _ function _ animation _ direction. xml
| Description |
Naming example |
| Fade in |
Main_button_fade_in.xml |
| Fade out |
Main_button_fade_out.xml |
| Push from below |
Button_push_down_in.xml |
| Launch from below |
Main_button_push_down_out.xml |
- Menu resource file (menu only has one type of resource, so you do not need to add a prefix to differentiate it ),Module _ function _ description. xml
| Description |
Naming example |
| Main interface menu |
Main_activity.xml |
| Fragment interface menu |
User_fragment.xml |
1. ids resources are mainly stored as the id value of the interface control, which is indicated by the lowercase name of the underline. Prefix:Prefix _Module _ function _ description,Common interface controls are named as follows:
| Description |
Naming example |
| 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 resource, which is in the format of no prefix or suffix and is in lowercase with an underscore,Module _ function _ description
3. The attributes and names of attrs/colors/ids use the Camel naming method. styles attributes use the Camel naming method and the names use the Pascal naming method. See the following figure:
<!--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"/>
If you have any questions, please leave a message, write a letter (gdgzhl@21cn.com) or follow my public account: