首页 > IT职业 > IT图书 > Android代码规范整理
个人主页:http://www.jianshu.com/u/9be0df6428d5
类命名方式采用
大驼峰命名法对于继承自安卓组件的类来说,类名应该以该组件名结尾,例如 :
SignInActivity,SignInFragment,ImageUploaderService,ChangePasswordDialog.对于工具类来说,命名方式应该以其完成功能开始,以
Utils结束 ,例如 :HttpUtils,ImageUtils.
资源文件以__小写加下划线__的方式命名.例如 :
R.layout.activity_main,
icon文件的命名规范
| Asset Type | Prefix 前缀 | Example |
|---|---|---|
| Icons | ic_ |
ic_star.png |
| Launcher icons | ic_launcher |
ic_launcher_calendar.png |
| Menu icons and Action Bar icons | ic_menu |
ic_menu_archive.png |
| Status bar icons | ic_stat_notify |
ic_stat_notify_msg.png |
| Tab icons | ic_tab |
ic_tab_recent.png |
| Dialog icons | ic_dialog |
ic_dialog_info.png |
选择器状态文件的命名规范
| State | Suffix 尾缀 | Example |
|---|---|---|
| Normal | _normal |
btn_order_normal.9.png |
| Pressed | _pressed |
btn_order_pressed.9.png |
| Focused | _focused |
btn_order_focused.9.png |
| Disabled | _disabled |
btn_order_disabled.9.png |
| Selected | _selected |
btn_order_selected.9.png |
布局文件的命名需要与他所嵌入的安卓组件匹配,但是将组件名称前移到开始处,例如,我们要创建一个名字为
SignInActivity, 其名字应该为activity_sign_in.xml.
| Component 组件 | Class Name | Layout Name |
|---|---|---|
| Activity | UserProfileActivity |
activity_user_profile.xml |
| Fragment | SignUpFragment |
fragment_sign_up.xml |
| Dialog | ChangePasswordDialog |
dialog_change_password.xml |
| AdapterView Item | --- | item_person.xml |
公有变量按 小驼峰 法命名
私有 & 非静态成员变量以 m 开头
私有 & 静态成员变量以 s 开头
常量以大写字母和下划线 _ 组成
尽量使用 功能/描述 + 类型 的模式 ,如 mNameTextView
类中变量的组件类型请不要使用缩写
注意不要使用 aa bb cc3 这种变态的命名方式 !!
类变量过多时请 分块摆放 并且 写好注释
接口类 请直接定义在类的最后
Example:
public class MyClass { //静态常量
public static final int SOME_CONSTANT = 42; //公有变量
public int publicField; //私有静态变量
private static MyClass sSingleton; //默认变量
int mPackagePrivate; //私有变量
private int mPrivate; //继承型变量
protected int mProtected;
}
类方法采用 小驼峰 命名法
根据函数所完成功能命名 , 如 changView()
在函数头写对于函数功能、参数和返回值的注释,如:
/**
* 获取两个数中最大的一个
*
* @param value1 参与比较的第一个数
* @param value2 参与比较的第二个数
* @return 两个参数中最大的一个数
*/public int max(int value1, int value2) { return (value1 > value2) ? value1 : value2;
}
一个函数请尽量保持在 50行 之内 !!
id 以 所在组件_类型_命名 的模式,例如: @+id/main_tv_name 、 @id/chat_btn_send
布局多处重用的请使用 标签
所有文本请定义在 strings.xml 中 , 如 @string/app_name
重用dp请定义在 dimens.xml 中 , 如 @dimen/entry_item_height
对应组件缩写表:
| Component 组件 | Abbreviation 缩写 |
|---|---|
| Fragment | fgm |
| TextView | tv |
| ImageView | iv |
| Button | btn |
| EditText | et |
| LinearLayout | ll |
| ReleativeLayout | rl |
| normally : FirstSecond | fs |
遵循
完整性规范性有序性原则
__分块并注释__, 将 使用在不同的 Activity 或者 Fragment 中的 xml 变量 进行分块
命名举例 :login_error_tips in strings.xml
login_error_tips_height in dimens.xmllogin_error_tips_bg in colors.xml
| Prefix 前缀 | Description 描述 |
|---|---|
error_ |
An error message |
msg_ |
A regular information message |
title_ |
A title, i.e. a dialog title |
action_ |
An action such as "Save" or "Create" |
| Good | Bad |
|---|---|
XmlHttpRequest |
XMLHTTPRequest |
getCustomerId |
getCustomerID |
String url |
String URL |
long id |
long ID |
This is good
if (condition){
body();
}
This is __bad__:
if (condition) body(); // bad!
This is __good__:
This is bad :
<!-- Don't do this! -->
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2231/viewspace-2814491/ ,如需转载,请注明出处,否则将追究法律责任。