Android 命名规范 (提高代码可以读性)

<p><span > 刚接触android的时候,命名都是按照拼音来,所以有的时候想看懂命名的那个控件什么是什么用的,就要读一遍甚至好几遍才知道,这样的话,在代码的审查和修改过程中就会浪费不少不必要的时间。如果就是我一个人开发,一个人维护的话还好,可是如果一个项目是团队分工合作,这样让你的同事去看你的代码就更加吃力了,因为大家之间的编程方式不一样,所以,在开发过程中,命名规范统一尤为重要,最好是团队中统一好大家命名方法,这样对于日后的工作会轻松很多。</span></p><p><span > 在面试的时候,审核一个程序员的编程水平的时候,命名规范也是一大标准,所以,下面我就总结下我从网上别人的经验和自己的感悟中收集总结的android命名规范,希望对大家有所帮助。</span></p><p><span > 提醒:在命名规范的统一下,在加上注释,这样日后维护会高效快捷好多。</span></p><p><span >先初略介绍下当前主要的标识符命名法和英文缩写规则:(这段别人那边借鉴的)</span><br/></p><p><span ></span></p><p ><span >在讲解命名规范前,先初略介绍下当前主要的标识符命名法和英文缩写规则。</span><span ><br/><strong><span ><br/></span></strong></span><strong><span >标识符命名法</span></strong><span ><br/></span><span >标识符命名法最要有四种</span><span >:</span><span ><br/><br/><span >1 </span></span><span >驼峰</span><span >(Camel)</span><span >命名法</span><span >:</span><span >又称小驼峰命名法,除首单词外,其余所有单词的第一个字母大写。</span><span ><br/><br/><span >2 </span></span><span >帕斯卡</span><span >(pascal)</span><span >命名法</span><span >:</span><span >又称大驼峰命名法,所有单词的第一个字母大写</span><span ><br/><br/><span >3 </span></span><span >下划线命名法</span><span >:</span><span >单词与单词间用下划线做间隔。</span><span ><br/><br/><span >4 </span></span><span >匈牙利命名法</span><span >:</span><span >广泛应用于微软编程环境中,在以</span><span >Pascal</span><span >命名法的变量前附加小写序列说明该变量的类型。</span><span ></span><span >量的取名方式为:</span><span >&lt;scope_&gt;+&lt;prefix_&gt;+&lt;qualifier&gt;</span><span >范围前缀,类型前缀,限定词。</span><span ><br/><br/></span><span >个人觉得标识符命名原则</span><span >:</span><strong><span >尽可能的用最少的字符而又能完整的表达标识符的含义。</span></strong><span ><br/><br/></span><span >英文缩写原则:</span><span ><br/><span >1 </span></span><span >较短的单词可通过去掉</span><span >&quot;</span><span >元音</span><span >&quot;</span><span >形成缩写</span><span ><br/><span >2 </span></span><span >较长的单词可取单词的头几个字母形成缩写</span><span ><br/><span >3 </span></span><span >此外还有一些约定成俗的英文单词缩写</span><span >.</span><span ><br/><br/></span><span >下面为常见的英文单词缩写</span><span >:</span></p><table border="1" width="493"><tbody><tr class="firstRow"><td ><p ><strong><span >名称</span></strong></p></td><td ><p ><strong><span >缩写</span></strong></p></td></tr><tr><td><p ><span >icon </span></p></td><td><p><span >ic</span><span >(主要用在</span><span >app</span><span >的图标</span><span >)</span></p></td></tr><tr><td><p ><span >color </span></p></td><td><p><span >cl</span><span >(</span><span >主要用于颜色值</span><span >)</span></p></td></tr><tr><td><p ><span >divider</span></p></td><td><p ><span >di</span><span >(</span><span >主要用于分隔线,不仅包括</span><span >Listview</span><span >中的</span><span >divider</span><span >,还包括普通布局中的线</span><span >)</span></p></td></tr><tr><td><p><span >selector</span></p></td><td><p ><span >sl</span><span >(</span><span >主要用于某一</span><span >view</span><span >多种状态,不仅包括</span><span >Listview</span><span >中的</span><span >selector</span><span >,还包括按钮的</span><span >selector</span><span >)</span></p></td></tr><tr><td><p ><span >average</span></p></td><td><p ><span >avg</span></p></td></tr><tr><td><p ><span >background</span><span ></span></p></td><td><p ><span >Bg</span><span >(</span><span >主要用于布局和子布局的背景</span><span >)</span></p></td></tr><tr><td><p ><span >buffer</span></p></td><td><p ><span >buf</span></p></td></tr><tr><td><p ><span >control</span></p></td><td><p ><span >ctrl</span></p></td></tr><tr><td><p><span >delete</span></p></td><td><p ><span >del</span></p></td></tr><tr><td><p><span >document</span></p></td><td><p ><span >doc</span></p></td></tr><tr><td><p><span >error</span></p></td><td><p ><span >err</span></p></td></tr><tr><td><p><span >escape</span></p></td><td><p ><span >esc</span></p></td></tr><tr><td><p><span >increment</span></p></td><td><p ><span >inc</span></p></td></tr><tr><td><p><span >infomation</span></p></td><td><p ><span >info</span></p></td></tr><tr><td><p><span >initial</span></p></td><td><p ><span >init</span></p></td></tr><tr><td><p><span >image</span></p></td><td><p ><span >img</span></p></td></tr><tr><td><p><span >Internationalization</span></p></td><td><p ><span >I18N</span></p></td></tr><tr><td><p><span >length</span></p></td><td><p ><span >len</span></p></td></tr><tr><td><p><span >library</span></p></td><td><p ><span >lib</span></p></td></tr><tr><td><p><span >message</span></p></td><td><p ><span >msg</span></p></td></tr><tr><td><p><span >password-</span></p></td><td><p ><span >pwd</span></p></td></tr><tr><td><p><span >position</span></p></td><td><p ><span >pos</span></p></td></tr><tr><td><p><span >server</span></p></td><td><p ><span >srv</span></p></td></tr><tr><td><p><span >string</span></p></td><td><p ><span >str</span></p></td></tr><tr><td><p><span >temp</span></p></td><td><p ><span >tmp</span></p></td></tr><tr><td><p><span >window</span></p></td><td><p ><span >wnd(win)</span></p></td></tr></tbody></table><p ><span ><br/></span><span >程序中使用单词缩写原则:<strong>不要用缩写,除非该缩写是约定俗成的。</strong></span></p><p ><span ><br/></span><span ><span >命名规范:</span></span><span ><br/><br/><span >1 </span></span><span >包(</span><span >packages</span><span >)</span><span >:</span><span >采用反域名命名规则,全部使用小写字母。一级包名为</span><span >com</span><span >,二</span><span >级包名为</span><span >xx(可以是公司或则个人的随便)</span><span >,三级包名根据应用进行命名,四级包名为模块名或层级名</span><br/><br/></p><table border="1" width="493"><tbody><tr class="firstRow"><td ><p ><strong><span >包名</span></strong></p></td><td ><p ><strong><span >此包中包含</span></strong></p></td></tr><tr><td><p ><span >com.xx.</span><span >应用名称缩写</span><span >.activities</span></p></td><td><p><span >页面用到的</span><span >Activity</span><span >类</span><span ></span><span >(activities</span><span >层级名</span>用户界面层<span >)</span></p></td></tr><tr><td><p ><span ></span>com.xx.<span >应用名称缩写</span><span >.base</span></p></td><td><p ><span >页面中每个</span><span >Activity</span><span >类共享的可以写成一个</span><span >i</span><span >额</span><span >BaseActivity</span><span >类</span><span >(</span><span >基础共享的类</span><span >)</span></p></td></tr><tr><td><p ><span >com.xx.</span><span >应用名称缩写</span><span >.adapter </span></p></td><td><p ><span >页面用到的</span><span >Ad</span><span ><span >apter</span><span >类</span><span >(</span><span >适</span></span><span >配器的类</span><span >)</span></p></td></tr><tr><td><p><span >com.xx.</span><span >应用名称缩写</span><span >.tools</span></p></td><td><p ><span >此包中包含:公共工具方法类</span>(<span >tools</span><span >模块名)</span></p></td></tr><tr><td><p ><span >com.xx.</span><span >应用名称缩写</span><span >.bean</span></p><p ><span >(</span><span >或则</span><span >com.xx.</span><span >应用名称缩写</span><span >.unity)</span></p></td><td><p ><span >此包中包含:元素类</span></p></td></tr><tr><td><p ><span >com.xx.</span><span >应用名称缩写</span><span >.db</span></p></td><td><p ><span >数据库操作类</span></p></td></tr><tr><td><p ><span >com.xx.</span><span >应用名称缩写</span><span >.view</span></p><p ><span >(</span><span >或则</span><span >com.xx.</span><span >应用名称缩写</span><span >.ui)</span></p></td><td><p ><span >自定义的</span><span >View</span><span >类等</span></p></td></tr><tr><td><p ><span >com.xx.</span><span >应用名称缩写</span><span >.service </span><span ></span></p></td><td><p ><span >Service</span><span >服务</span></p></td></tr><tr><td><p><span >com.xx.</span><span >应用名称缩写</span><span >.broadcast </span></p></td><td><p ><span >Broadcast</span><span >服务</span></p></td></tr></tbody></table><p><span ></span><span ><br/></span></p><p ><span ><br/><span >2 </span></span><span >类(</span><span >classes</span><span >):名词,采用大驼峰命名法,尽量避免缩写,除非该缩写是众所周知的,</span><span ><span ></span></span><span ></span><span >比如</span><span >HTML,URL</span><span >,如果类名称中包含单词缩写,则单词缩写的每个字母均应大写。</span><span ><br/><br/></span></p><table border="1" width="553"><tbody><tr class="firstRow"><td ><p ><strong><span >类</span></strong></p></td><td ><p ><strong><span >描述</span></strong></p></td><td ><p ><strong><span >例如</span></strong></p></td></tr><tr><td><p ><span >activity</span><span ></span><span >类</span></p></td><td><p ><span >Aty</span><span >或者</span><span >Activity</span><span >为后缀标识</span></p></td><td><p ><span >欢迎页面类</span><span >WelcomeAty.</span><span >或者</span><span >WelcomeActivity</span></p></td></tr><tr><td><p ><span >Adapter</span><span >类</span></p></td><td><p ><span ></span>Adp<span >或者</span><span >Adapte</span><span >为后缀标识</span></p></td><td><p ><span >新闻详情适配器</span><span >NewtDetailAdp</span><span >或则直接</span><span > NewDetailAdapter</span></p></td></tr><tr><td><p ><span >解析类</span><span ></span></p></td><td><p ><span ></span><span >Hlr</span><span >为后缀标识</span></p></td><td><p ><span >首页解析类</span><span >HomePosterHlr</span></p></td></tr><tr><td><p ><span >公共方法类</span></p></td><td><p ><span ></span><span >Tools</span><span >或</span><span >Manager</span><span >为后缀标识</span></p></td><td><p ><span >线程池管理类:</span><span >ThreadPoolManager</span><span ><br/></span><span ></span><span >日志工具类:</span><span >LogTools</span></p></td></tr><tr><td><p ><span >数据库类</span></p></td><td><p ><span >以</span><span >DBHelper</span><span >后缀标识</span></p></td><td><p ><span >新闻数据库:</span><span >NewDBHelper</span></p></td></tr><tr><td><p ><span >Service</span><span >类</span></p></td><td><p ><span >以</span><span >Service</span><span >为后缀标识</span></p></td><td><p ><span >时间服务</span><span >Time</span><span >Service</span></p></td></tr><tr><td><p ><span >BroadcastReceive</span><span >类</span></p></td><td><p ><span > 以</span><span >Broadcast</span><span >为后缀标识</span></p></td><td><p ><span >时间通知</span><span >Time</span><span >Broadcast</span></p></td></tr><tr><td><p ><span >ContentProvider</span></p></td><td><p ><span ></span><span >以</span><span >Provider</span><span >为后缀标识</span></p></td><td><p ></p></td></tr><tr><td><p ><span >直接写的共享基础类</span></p></td><td><p ><span >以</span><span >Base</span><span >开头</span></p></td><td><p ><span >BaseActivity,BaseFragment</span></p></td></tr></tbody></table><p><br/><span ><span >3 </span></span><span >接口(</span><span >interface</span><span >):命名规则与类一样采用大驼峰命名法,多以</span><span >able</span><span >或</span><span >ible</span><span >结尾,如</span><span >interface Runn</span><span >a</span><span ></span><span >ble</span><span ></span><span >;</span><span ><br/><span ></span></span><span ></span><span >interface Access</span><span >ible</span><span ></span><span >。</span><span ><br/><br/><span >4 </span></span><span >方法(</span><span >methods</span><span >):动词或动名词,采用小驼峰命名法</span><span >例如</span><span >:onCreate(),run()<br/></span><span ><br/></span></p><table border="1" width="616"><tbody><tr class="firstRow"><td ><p ><strong><span >方法</span></strong></p></td><td ><p ><strong><span >说明</span></strong></p></td></tr><tr><td><p ><span >initXX()</span></p></td><td><p ><span ></span><span >初始化相关方法</span><span >,</span><span >使用</span><span >init</span><span >为前缀标识,如初始化布局initView()</span></p></td></tr><tr><td><p ><span >isXX()</span></p></td><td><p ><span ></span><span >checkXX()</span><span >方法返回值为</span><span >boolean</span><span >型的请使用</span><span >is</span><span >或</span><span >check</span><span >为前缀标识</span></p></td></tr><tr><td><p ><span >getXX()</span></p></td><td><p ><span >返回某个值的方法,使用</span><span >get</span><span >为前缀标识</span></p></td></tr><tr><td><p ><span >processXX()</span><span ></span></p></td><td><p ><span >对数据进行处理的方法,尽量使用</span><span >process</span><span >为前缀标识</span></p></td></tr><tr><td><p ><span >displayXX()</span></p></td><td><p ><span >弹出提示框和提示信息,使用</span><span >display</span><span >为前缀标识</span></p></td></tr><tr><td><p ><span >saveXX()</span></p></td><td><p ><span ></span><span >与保存数据相关的,使用</span><span >sav</span><span >为</span><span >e</span><span >前缀标识</span></p></td></tr><tr><td><p ><span >resetXX()</span></p></td><td><p ><span ></span><span >对数据重组的,使用</span><span >reset</span><span >前缀标识</span></p></td></tr><tr><td><p ><span >clearXX()</span></p></td><td><p ><span >清除数据相关的</span></p></td></tr><tr><td><p ><span >removeXXX()</span></p></td><td><p ><span >清除数据相关的</span></p></td></tr><tr><td><p ><span >drawXXX()</span></p></td><td><p ><span >绘制数据或效果相关的,使用</span><span >draw</span><span >前缀标识</span></p></td></tr></tbody></table><p><br/><br/><span >5 </span><span >变量(</span><span >variables</span><span >)采用小驼峰命名法。类中控件名称必须与</span><span >xml</span><span >布局</span><span >id</span><span >保持一致。</span><span ><br/><br/></span><span >用统一的量词通过在结尾处放置一个量词,就可创建更加统一的变量,它们更容易理解,也更容易搜索。例如,请使用</span><span >strCustomerFirst</span><span >和</span><span >strCustomerLast</span><span >,而不要使用</span><span >strFirstCustomer</span><span >和</span><span >strLastCustomer</span><span >。</span><span ><br/></span><span >量词列表:量词后缀说明</span><span ><br/><span >First</span></span><span ></span><span >一组变量中的第一个</span><span ><br/><span >Last</span></span><span ></span><span >一组变量中的最后一个</span><span ><br/><span >Next</span></span><span ></span><span >一组变量中的下一个变量</span><span ><br/><span >Prev</span></span><span ></span><span >一组变量中的上一个</span><span ><br/><span >Cur</span></span><span ></span><span >一组变量中的当前变量</span><span ><br/><span ></span><br/><span >6 </span></span><span >常量(</span><span >Constants</span><span >)全部大写</span><span >,</span><span >采用下划线命名法</span><span >.</span><span >例如:</span><span >MIN_WIDTH</span><span ><br/><br/></span><span ><br/><span >7 </span></span><span >资源文件(图片drawable文件夹下):</span><span >全部小写,采用下划线命名法,加前缀区分</span></p><p ><span ></span></p><p ><span >命名模式:</span><span >activity</span><span >名称</span><span >_</span><span >逻辑名称</span><span >/common_</span><span >逻辑名称</span></p><p ><span ><span >如果有多种形态如按钮等除外如</span><strong><span ><span >btn_xx.</span>xml</span><span >(</span><span >selector</span><span >)</span></strong><br/></span></p><table border="1" width="493"><tbody><tr class="firstRow"><td ><p ><strong><span >名称</span></strong></p></td><td ><p ><strong><span >功能</span></strong></p></td></tr><tr><td><p ><span >btn_xx</span></p></td><td><p ><span >按钮图片使用btn_整体效果<span ><strong>(</strong></span><span ><strong>selector</strong></span><span ><strong>)</strong></span></span></p></td></tr><tr><td><p ><span >btn_xx_normal</span></p></td><td><p ><span >按钮图片使用btn_正常情况效果</span></p></td></tr><tr><td><p ><span >btn_xx_press</span></p></td><td><p ><span >按钮图片使用btn_点击时候效果</span></p></td></tr><tr><td><p ><span >bg_head</span></p></td><td><p ><span >背景图片使用bg_功能_说明</span></p></td></tr><tr><td><p ><span >def_search_cell</span></p></td><td><p ><span >默认图片使用def_功能_说明</span></p></td></tr><tr><td><p ><span >icon_more_help</span></p></td><td><p ><span >图标图片使用icon_功能_说明</span></p></td></tr><tr><td><p ><span >seg_list_line</span></p></td><td><p ><span >具有分隔特征的图片使用seg_功能_说明</span></p></td></tr><tr><td><p ><span >sel_ok</span></p></td><td><p ><span >选择图标使用sel_功能_说明</span></p></td></tr></tbody></table><p ><span >命名后缀:</span></p><p ><span ></span></p><table border="1" align="left" width="604"><tbody><tr class="firstRow"><td ><p ><strong><span >后缀</span></strong></p></td><td ><p ><strong><span >说明</span></strong></p></td></tr><tr><td><p ><span >unit</span></p></td><td><p ><span ><span >在使用</span><span >xml</span><span >的</span><span >tilemode</span><span >来配图片时,</span><span >element</span><span >图片使用此后缀</span></span></p></td></tr><tr><td><p ><span >nor</span></p></td><td><p ><span >图片的状态,代表普通状态</span></p></td></tr><tr><td><p ><span >hl</span></p></td><td><p ><span >图片的状态,代表高亮状态</span></p></td></tr><tr><td><p ><span >press</span></p></td><td><p ><span >图片的状态,代表按下状态</span></p></td></tr><tr><td><p ><span >select</span></p></td><td><p ><span ><span >图片的状态,代表其所占的</span><span >view</span><span >被选中</span></span></p></td></tr><tr><td><p ><span >unselect</span></p></td><td><p ><span ><span >图片的状态,代表其所占的</span><span >view</span><span >没有被选中</span></span></p></td></tr></tbody></table><p ><span ></span></p><p ><strong><span ></span></strong></p><p ><span ></span></p><p ><span ></span></p><p ><span ></span></p><p ><span ></span></p><p ><span ></span></p><p ><span ></span></p><p ><span ></span></p><p ><span ></span></p><p><span >8</span><span ></span><span ></span><span ></span><span >资源布局文件</span>(XML文件(layout布局文件)):</p><p >全部小写,采用下划线命名法</p><p ><span >1)</span><span >.</span><span >contentview</span><span >命名,<span >Activity</span><span >默认布局,以去掉后缀的</span><span >Activity</span><span >类进行命名。不加后缀</span>:</span></p><p ><span ></span></p><p ><strong><span ><span > </span>功能模块.xml<br/></span></strong></p><p ><span ><span > 例如:<span ><span >main.xml</span><span >、</span><span >more.xml、settings.xml</span></span></span></span></p><p> 或则:activity_<span >功能模块</span><span >.xml</span></p><p ><span > 例如:</span><strong><span >activity_main.xml</span><span >、</span><span >activity_more.xml</span></strong></p><p ><span >2)</span><span >.</span><span >Dialog</span><span >命名:</span><span >dialog_</span><span >描述</span><span >.xml</span></p><p ><span ></span><span >例如:</span><strong><span >dlg_hint.xml</span></strong></p><p ><span >2)</span><span >.</span><span >PopupWindow</span><span >命名:</span><span >ppw_</span><span >描述</span><span >.xml</span></p><p ><span ></span><span >例如:</span><strong><span >ppw _info.xml</span></strong></p><p ><span >3).</span><span >列表项命名</span><span >listitem_</span><span >描述</span><span >.xml</span></p><p ><span ></span><span >例如:</span><strong><span >listitem_city.xml</span></strong></p><p ><span >4)</span><span >.包含项:</span><span >include_</span><span >模块</span><span >.xml</span></p><p ><span ></span><span >例如:</span><strong><span >include_head.xml</span><span >、</span><span >include_bottom.xml</span></strong></p><p ><span ><span ></span></span></p><p ><span >5)</span><span >.adapter的子布局:</span><span ><span ><strong>功能模块</strong></span>_</span><span >item</span><span >.xml</span></p><p ><span ></span><span >例如:</span><strong><span >main_item.xml</span><span >、</span></strong></p><p ><span ><br/></span></p><p ><span >9</span><span ></span><span ></span><span ></span><span >动画文件(anim文件夹下</span>):全部小写,采用下划线命名法,加前缀区分。</p><p ><span ></span></p><p>//前面为动画的类型,后面为方向</p><table border="1" width="481"><tbody><tr class="firstRow"><td ><p ><strong><span >动画命名例子</span></strong></p></td><td ><p ><strong><span >规范写法</span></strong></p></td><td ><p ><strong><span >备注</span></strong></p></td></tr><tr><td><p >fade_in</p></td><td><p ><span >淡入</span></p></td><td><p ><span ></span></p></td></tr><tr><td><p >fade_out</p></td><td><p >淡出</p></td><td><p ><span ></span></p></td></tr><tr><td><p >push_down_in</p></td><td><p >从下方推入</p></td><td><p ><span ></span></p></td></tr><tr><td><p >push_down_out</p></td><td><p >从下方推出</p></td><td><p ><span ></span></p></td></tr><tr><td><p >push_left</p></td><td><p >推像左方</p></td><td><p ><span ></span></p></td></tr><tr><td><p >slide_in_from_top</p></td><td><p >从头部滑动进入</p></td><td><p ><span ></span></p></td></tr><tr><td><p >zoom_enter</p></td><td><p >变形进入</p></td><td><p ><span ></span></p></td></tr><tr><td><p >slide_in</p></td><td><p >滑动进入</p></td><td><p ></p></td></tr><tr><td><p >shrink_to_middle</p></td><td><p >中间缩小</p></td><td><p ></p></td></tr></tbody></table><p><span >10</span><span ></span><span ></span><span >资源ID</span>(resourcesid):大小写规范与方法名一致,采用小驼峰命名法。命名规范为&quot;资源控件的缩写<span ></span><span >名&quot;+</span>&quot;变量名&quot;。注意:<strong><span >页面控件名称应该和控件id</span>名保持一致</strong></p><p><strong><span ><span >strings.xml,colors.xml等</span><span >中的</span><span >id</span><span >命名:</span></span></strong></p><p><span ><span ><span ></span></span></span></p><p ><span >命名模式:</span><span >activity</span><span >名称</span><span >_</span><span >功能模块名称</span><span >_</span><span >逻辑名称</span><span >/activity</span><span >名称</span><span >_</span><span >逻辑名称</span><span >/common_</span><span >逻辑名称</span></p><p ><span >strings.xml</span><span >中,使用</span><span >activity</span><span >名称注释,将文件内容区分开来</span></p><p ><br/></p><p ><span >11 layout</span><span >中的</span><span >id</span><span >命名</span></p><p ><span >命名模式为:</span><span >view</span><span >缩写</span><span >_</span><span >模块名称</span><span >_view</span><span >的逻辑名称</span></p><p ><span >view</span><span >的缩写详情如下:</span></p><p ><br/></p><table border="1" align="left" width="493"><tbody><tr class="firstRow"><td ><p ><strong><span >控件</span></strong></p></td><td ><p ><strong><span >缩写</span></strong></p></td></tr><tr><td><p ><span >LayoutView</span></p></td><td><p ><span >lv</span></p></td></tr><tr><td><p ><span >RelativeView</span></p></td><td><p ><span >rv</span></p></td></tr><tr><td><p ><span >TextView</span></p></td><td><p ><span >tv</span></p></td></tr><tr><td><p ><span >Button</span></p></td><td><p ><span >btn</span></p></td></tr><tr><td><p ><span >ImageButton</span></p></td><td><p ><span >imgBtn</span></p></td></tr><tr><td><p ><span >ImageView</span></p></td><td><p ><span ><span >mgView</span><span >或则</span><span > iv</span></span></p></td></tr><tr><td><p ><span >CheckBox</span></p></td><td><p ><span >chk</span></p></td></tr><tr><td><p ><span >RadioButton</span></p></td><td><p ><span >rdoBtn</span></p></td></tr><tr><td><p ><span >analogClock</span></p></td><td><p ><span >anaClk</span></p></td></tr><tr><td><p ><span >DigtalClock</span></p></td><td><p ><span >dgtClk</span></p></td></tr><tr><td><p ><span >DatePicker</span></p></td><td><p ><span >dtPk</span></p></td></tr><tr><td><p ><span >EditText </span></p></td><td><p ><span >edtTxt</span></p></td></tr><tr><td><p ><span >TimePicker </span></p></td><td><p ><span >tmPk</span></p></td></tr><tr><td><p ><span >toggleButton </span></p></td><td><p ><span >tglBtn</span></p></td></tr><tr><td><p ><span >ProgressBar</span></p></td><td><p ><span >proBar</span></p></td></tr><tr><td><p ><span >SeekBar</span></p></td><td><p ><span >skBar</span></p></td></tr><tr><td><p ><span >AutoCompleteTextView</span></p></td><td><p ><span >autoTxt</span></p></td></tr><tr><td><p ><span >ZoomControls</span></p></td><td><p ><span >zmCtl</span></p></td></tr><tr><td><p ><span >VideoView</span></p></td><td><p ><span >vdoVi</span></p></td></tr><tr><td><p ><span >WdbView</span></p></td><td><p ><span >webVi</span></p></td></tr><tr><td><p ><span >RantingBar</span></p></td><td><p ><span >ratBar</span></p></td></tr><tr><td><p ><span >Tab</span></p></td><td><p ><span >tab</span></p></td></tr><tr><td><p ><span >Spinner</span></p></td><td><p ><span >spn</span></p></td></tr><tr><td><p ><span >Chronometer</span></p></td><td><p ><span >cmt</span></p></td></tr><tr><td><p ><span >ScollView</span></p></td><td><p ><span >sclVi</span></p></td></tr><tr><td><p ><span >TextSwitch</span></p></td><td><p ><span >txtSwt</span></p></td></tr><tr><td><p ><span >ImageSwitch</span></p></td><td><p ><span >imgSwt</span></p></td></tr><tr><td><p ><span >listView</span></p></td><td><p ><span ><span >lVi</span><span >或则</span><span >lv</span></span></p></td></tr><tr><td><p ><span >ExpandableList</span></p></td><td><p ><span >epdLt</span></p></td></tr><tr><td><p ><span >MapView</span></p></td><td><p ><span >mapVi</span></p></td></tr></tbody></table><p></p><p></p><p></p><p ><br/></p><p ><span ><br/></span></p><p ><span ><br/></span></p><p ><span ><br/></span></p><p ><span ><br/></span></p><p ><span ><br/></span></p><p ><span ><br/></span></p><p ><span ><br/></span></p><p ><span ><br/></span></p><p ><span ><br/></span><span >12.activity</span><span >中的</span><span >view</span><span >变量命名</span></p><p ><span >命名模式为:逻辑名称</span><span >+view</span><span >缩写</span></p><p ><span >建议:如果</span><span >layout</span><span >文件很复杂,建议将</span><span >layout</span><span >分成多个模块,每个模块定义一个</span><span >moduleViewHolder</span><span >,其成员变量包含所属</span><span >view</span></p><p ><span >13.styles.xml</span><span >:将</span><span >layout</span><span >中不断重现的</span><span >style</span><span >提炼出通用的</span><span >style</span><span >通用组件,放到</span><span >styles.xml</span><span >中;</span></p><p ><span >14.</span><span >使用</span><span >layer-list</span><span >和</span><span >selector</span></p><p ><span ><br/></span></p><p ><span ></span></p><p ><span ><span >Android</span><span >编码规范建议(别人弄的觉得蛮有道理)</span></span></p><p ><span >1.java</span><span >代码中不出现中文,最多注释中可以出现中文</span></p><p ><span >2.</span><span >局部变量命名、静态成员变量命名</span></p><p ><span >只能包含字母,单词首字母除第一个外,都为大写,其他字母都为小写</span></p><p ><span >3.</span><span >常量命名</span></p><p ><span >只能包含字母和</span><span >_</span><span >,字母全部大写,单词之间用</span><span >_</span><span >隔开</span></p><p ><span >4.</span><span >图片尽量分拆成多个可重用的图片</span></p><p ><span >5.</span><span >服务端可以实现的,就不要放在客户端</span></p><p ><span >6.</span><span >引用第三方库要慎重,避免应用大容量的第三方库,导致客户端包非常大</span></p><p ><span >7.</span><span >处理应用全局异常和错误,将错误以邮件的形式发送给服务端</span></p><p ><span >8.</span><span >图片的</span><span >.9</span><span >处理</span></p><p ><span >9.</span><span >使用静态变量方式实现界面间共享要慎重</span></p><p ><span >10.Log(</span><span >系统名称</span><span >模块名称</span><span >接口名称,详细描述</span><span >)</span></p><p ><span >11.</span><span >单元测试(逻辑测试、界面测试)</span></p><p ><span >12.</span><span >不要重用父类的</span><span >handler</span><span >,对应一个类的</span><span >handler</span><span >也不应该让其子类用到,否则会导致</span><span >message.what</span><span >冲突</span></p><p ><span >13.activity</span><span >中在一个</span><span >View.OnClickListener</span><span >中处理所有的逻辑</span></p><p ><span >14.strings.xml</span><span >中使用</span><span >%1$s</span><span >实现字符串的通配</span></p><p ><span >15.</span><span >如果多个</span><span >Activity</span><span >中包含共同的</span><span >UI</span><span >处理,那么可以提炼一个</span><span >CommonActivity</span><span >,把通用部分叫由它来处理,其他</span><span >activity</span><span >只要继承它即可</span></p><p ><span >16.</span><span >使用</span><span >button+activitgroup</span><span >实现</span><span >tab</span><span >效果时,使用</span><span >Button.setSelected(true)</span><span >,确保按钮处于选择状态,并使</span><span >activitygroup</span><span >的当前</span><span >activity</span><span >与该</span><span >button</span><span >对应</span></p><p ><span >17.</span><span >如果所开发的为通用组件,为避免冲突,将</span><span >drawable/layout/menu/values</span><span >目录下的文件名增加前缀</span></p><p ><span >18.</span><span >数据一定要效验,例如</span></p><p ><span >字符型转数字型,如果转换失败一定要有缺省值;</span></p><p><span >服务端响应数据是否有效判断</span></p>
RangeTime:0.006486s
RangeMem:235.56 KB
返回顶部 留言