Android 代码规范

作者: Bear_android | 来源:发表于2017-05-03 15:43 被阅读106次

1 文件命名

1.1 类文件命名 参考.

类命名方式采用 大驼峰 命名法

对于继承自安卓组件的类来说,类名应该以该组件名结尾,例如 : SignInActivity, SignInFragment, ImageUploaderService, ChangePasswordDialog.

对于工具类来说,命名方式应该以其完成功能开始,以 Utils 结束 ,例如 :HttpUtils , ImageUtils.

1.2 资源文件

资源文件以小写加下划线的方式命名.例如 :R.layout.activity_main ,

1.3 Drawable文件

  • 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

1.4 布局文件

布局文件的命名需要与他所嵌入的安卓组件匹配,但是将组件名称前移到开始处,例如,我们要创建一个名字为 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

1.5 类变量命名

  • 公有变量按 小驼峰 法命名
  • 私有 & 非静态成员变量以 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;
}

1.6 类方法命名

  • 类方法采用 小驼峰 命名法
  • 根据函数所完成功能命名 , 如 changView()
  • 在函数头写对于函数功能、参数和返回值的注释,如:
   /**
    * 获取两个数中最大的一个
    *
    * @param value1 参与比较的第一个数
    * @param value2 参与比较的第二个数
    * @return 两个参数中最大的一个数
    */
   public int max(int value1, int value2) {
       return (value1 > value2) ? value1 : value2;
   }
  • 一个函数请尽量保持在 50行 之内 !!

1.7 布局文件变量命名

  • id所在组件_类型_命名 的模式,例如: @+id/main_tv_name@id/chat_btn_send
  • 布局多处重用的请使用 <include> 标签
  • 所有文本请定义在 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

1.8 strings.xml dimens.xml colors.xml xml变量命名

  • 遵循 完整性 规范性 有序性原则
  • 分块并注释, 将 使用在不同的 Activity 或者 Fragment 中的 xml 变量 进行分块
  • 命名举例 :
    login_error_tips in strings.xml
    login_error_tips_height in dimens.xml
    login_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"

1.9 额外注意

Good Bad
XmlHttpRequest XMLHTTPRequest
getCustomerId getCustomerID
String url String URL
long id long ID

2 代码规范


This is good

if (condition){
    body();
}

This is bad:

if (condition) body();  // bad!

This is good:

<TextView
    android:id="@+id/text_view_profile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

This is bad :

<!-- Don't do this! -->
<TextView
    android:id="@+id/text_view_profile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TextView>

相关文章

  • Android编码规范

    Android开发代码规范相关系列文章: Android命名规范 Android编码规范 Android编码规范有...

  • Android命名规范

    Android开发代码规范相关系列文章: Android命名规范 Android编码规范 俗话说:无规矩不成方圆,...

  • Android开发中的优化方案

    一、命名规范 代码规范先从命名规范开始,Android的命名规范主要涉及:Java源代码,xml文件,图片资源。 ...

  • Android:你不能忽略的代码命名规范

    前言 Android代码规范内容非常多,但对我们最有用& 最有影响的莫过于 Android代码的命名规范 可是,...

  • Android代码规范

    1.前言 本文档参考了Google官方Android编码风格规范,尽量形成一个统一的风格,见量知其意就可。 2.源...

  • Android代码规范

    框架要求 : 1 . 编码格式统一采用 UTF-8 , 开发工具统一使用 androidstudio . java...

  • Android 代码规范

    本文转载于Blankj/AndroidStandardDevelop: :star2: Best practice...

  • Android代码规范

    一、Android组件 通信,对于数据量比较大的,避免使用 Intent + Parcelable的方式,可以考虑...

  • Android代码规范

    在刚开始编程的时候,代码毫无规范而言,命名则是根据自己的喜好而来,记得英文的时候直接上英文,忘记的时候直接拼音,有...

  • Android代码规范

    https://blog.csdn.net/xinzailiulang992/article/details/53...

网友评论

    本文标题:Android 代码规范

    本文链接:https://www.haomeiwen.com/subject/qtzktxtx.html