The naming rules for Android development that you must not read

Source: Internet
Author: User

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:
  • Packages): Use anti-Domain Name naming rules, all use lower-case letters. The first-level package name is a local top-level domain name such as com, and the second-level package name is xx (which can be a company or an individual's casual). The third-level package name is named Based on the application, and the fourth-level package name is a module name or level; such as com. tinyx. myapp. activities;
  • Class (classes ):Use the Pascal naming method to avoid abbreviations. For example, MyActivity is a well-known abbreviation, such as HTML and URL. If a class name contains an abbreviation, each letter of the abbreviation should be capitalized, for example: publicHTML, CommonURL.
  • Interface ):Like a class, it uses the Pascal naming method and ends with "able" or "visible". It uses multiple methods to indicate behaviors, such as Runnable and Accessible;
  • Method (methods ):The verb or noun uses the Camel naming method, such as onCreate () and run (). Below are some suggestions:

    • Initialize related methods and use init as the prefix, for example, initializing the layout of initView ();
    • The boolean type is identified by the "is" or "check" prefix, such as checkValue () and isValidate ();
    • Return the method of a value. Use get as the prefix, for example, getName ();
    • Process data. Use process as the prefix, for example, processUpdate ();
    • Save data. Use save as the prefix, for example, saveData ();
    • For data resetting, use the reset prefix identifier, for example, resetData ();
    • Clear data. Use the clear prefix to identify the data, such as clearData ();
    • Remove some items and use the remove prefix identifier, such as removeItem ();
    • If data or effects are drawn, the draw prefix is used for identification, for example, drawCircle ();
  • Variable (variables ):We recommend that you use meaningful names such as firstName and lastName. The above rules are used by default for model variables.
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
  • Values resources:

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:

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.