Android代码书写规范
1.资源文件命名规则
2.类名文件命名规则
3.尽量少用枚举
4.public方法、重要逻辑、主要类结构体必须注释,其他部分可自定注释
5.提交代码必须描述清楚修改内容,如果一次提交内容过多,拆分功能进行多次提交,尽量保持每次提交功能修改单一原则
6.类文件尽量不超过300行,方法尽量不超过一个屏幕
7.切忌在功能未完成时做过多类文件的重构
8.坚持以上7点,成为优秀码农 ############################################################
# Android Coding Standards #
# #
# https://github.com/47deg/coding-guidelines/tree/master/java/android#android-coding-standards #
############################################################ 1. Naming Conventions Developers should pay special attention to these naming conventions as they differ from those in the standard Java Coding Conventions. 1.1. Common Resource Files The folder values will have different files that will store information for our project. Some of the most common files and their name are colors.xml: Colors used in the application
config.xml: Stores information to configure our project (ex. keys for services, urls, etc)
dimen.xml: Dimensions used in application (ex. action bar height , paddings, etc)
strings.xml: Localizable strings
plurals.xml: Plurals. Contains references to strings.xml
arrays.xml: Arrays. Contains references to strings.xml
1.2. Java Packages & Class Names An android App should generally follow the following package structure com.company.product.android
activities: All Activities with the word Activity pre-fixed by the Activity name: *[Name]*Activity e.g. MainActivity
adapters: All Adapters with the word Adapter pre-fixed by the Adapter name: *[Name]*Adapter e.g. UserListAdapter
services: All Services including API clients and other persistence related services e.g. UserService
components: All reusable components utilized in Activity and Fragments e.g. UserProfileComponent
dialogs: All Fragment Dialogs with the word Dialog pre-fixed by the dialog name: *[Name]*Dialog e.g. DeleteAccountConfirmationDialog
fragments: All Fragments with the word Fragment pre-fixed by the Fragment name: *[Name]*Fragment e.g. UserMapLocationFragment
utils: All cross package utilities with the word Utils pre-fixed by the Utility name: *[Name]*Utils e.g. StringUtils
1.3. Resource Names The following structure should be followed when naming resoures. group _ type _ name _ [state] _ [suffix] Group: Application area or screen. If the resource is used in different parts of applications 'common' should be used instead. e.g. actionbar, menu, media, popup, footer, audio, etc.
Type: Resource Type. e.g. background, icon, button, textfield, list, menuitem, radiobutton, checkbox, tab, dialog, title, etc.
Name: Descriptive name as to what the resource is about. e.g. play, stop.
State: (Optional): The optional state of a parent resource. e.g. A button could be in 'normal', 'pressed', 'disabled' and 'selected'. A checkbox could be 'on' or 'off'. These resources should NEVER be used directly in layout but rather as state selectors.
Suffix: (Optional): An arbitrary suffix that helps to further identify a property of the resource. e.g. bright, dark, opaque, layer.
Below are some examples of properly named resources. common_background_app
audio_icon_play_on
common_icon_preferences
actionbar_button_send (XML resource)
action_button_send_normal
action_button_send_pressed
action_button_send_disabled
1.4. String Resources String resources placed in xml resources files such as strings.xml, config.xml, etc. are named following the same convention as Java naming conventions for variables and fields. CamelCase with the first letter lowercased. Below are some examples of properly named string identifiers. serverApiUrl
phoneNumber
services
url
1.5. Style Resources String resources placed in styles.xml are named in CamelCase. The following structure should be followed when naming style resoures. [Group]TypeName[Suffix] Group (Optional): Application area or screen. e.g. actionbar, menu, media, popup, footer, audio.
Type: Resource Type. e.g. Background, Icon, Button, Textfield, List, MenuItem, RadioButton, Checkbox, Tab, Dialog.
Name: Descriptive name as to what the resource is about. e.g. play, stop.
Suffix: (Optional): An arbitrary suffix that helps to further identify a property of the resource. e.g. Bright, Dark, Opaque, Layer.
Below are some examples of properly named string identifiers. ButtonSend
ActionBarButtonBack
ListTitle
1.6. Dimen Resources Dimens resources placed in dimen.xml. The following structure should be followed when naming dimentions. property _ default _ group _ type _ name property: Type of property reference. e.g. font_size, padding, margin, height, width.
default (Optional): Write "default" if is a general dimen.
group (Optional): Application area or screen. e.g. action_bar, menu, popup, wizard.
type (Optional): Type of resource. e.g. button, title, text, edittext.
name (Optional): Only if is necessary.
Below are some examples. padding_default
font_size_action_bar_button
height_default_action_bar
We should have some dimension in all projects by default. These are: padding_default
margin_default
font_size_default_button
font_size_default_title
font_size_default_text
height_default_action_bar
font_size_default_action_bar
2. Conventions for 7" devices 7" devices require a special treatment. The problem is that these devices usually use "mdpi" as default density. These are the consideration to follow when targeting apps in these devices Copy hdpi drawables to "drawable-sw600dp-mdpi".
Create a new dimensions file for this screens. The file "dimen.xml" will be in the "values-sw600dp-mdpi" folder. Usually the dimensions will be at 150%. All fonts sizes should be in dimen.xml file as well.
资源命名准则:
==============================================
drawable:
{group _ type _ name _ [state] _ [suffix]}
==============================================
id:
{group _ ui _ type _ [local] _ name}
==============================================
layout:
{[group] _ ui _ name}
==============================================
menu:
{[group] _ ui _ name}
==============================================
anim:
{group _ name _ [local]}
==============================================
string:
{group _ [ui] _ name}
==============================================
dimen:
{group _ name _ property _ [size]}
==============================================
**********************************************
group:
[common|uikit|sdk|app] ui:
[main|kitchen|home|activity|fragment|view|actionbar|...] type:
[bg(background)|ic(icon)|bt(button(必须有状态))|txt(textfield)
|list(listview)|menu(menuitem)|radio(radiobutton)|checkbox|...] local:
[top|head|bottom|left|right|in|out|rotate|...] property:
[font|padding|margin|height|width|...] size:
[large|big|normal|small|double|treble|...] state:
[normal|pressed|disabled|on|off|...] suffix:
[light|dark|...]
==============================================
**********************************************
Android代码书写规范的更多相关文章
- WEB标准:标准定义、好处、名词解释、常用术语、命名习惯、浏览器兼容、代码书写规范
1. WEB标准是什么? “WEB标准”是一系列标准的总称.一般的误区经常把WEB标准说成DIV+CSS.准确的说法应该是:采用W3C推荐的WEB标准中的XHTML1.1结合CSS2.0 样式表制作页 ...
- (转)Java代码书写规范
0. 安装阿里代码规范的eclipse插件 https://www.cnblogs.com/caer/p/7753522.html 1.基本原则 强制性原则: 1.字符串的拼加操作,必须使用S ...
- C#中的代码书写规范以及命名规范
C#代码书写规则: 1. 尽量使用接口,然后使用类实现接口,以提高程序的灵活性. 2.一行不要超过80个字符 3.尽量不要手动更改计算机生成的代码 4.关键的语句写注释 5.建议局部变量在最接近使用它 ...
- Unity项目代码书写规范
以Google的代码规范为主,稍加改动 https://google.github.io/styleguide/csharp-style.html 书写规范 基础写法 Pascal和驼峰混用,参数用驼 ...
- C++代码书写规范——给新手程序员的一些建议
代码就是程序员的面子,无论是在工作中在电脑上写程序代码还是在面试时在纸上写演示代码我们都希望写出整洁,优雅的代码.特别在工作中当我们碰到需要维护别人的代码,或者是多人参与一个项目大家一起写代码的时候, ...
- 【转】JavaScript常用代码书写规范
javascript 代码规范 代码规范我们应该遵循古老的原则:“能做并不意味着应该做”. 全局命名空间污染 总是将代码包裹在一个立即的函数表达式里面,形成一个独立的模块. 不推荐 1 2 3 var ...
- JavaScript常用代码书写规范
javascript 代码规范 代码规范我们应该遵循古老的原则:“能做并不意味着应该做”. 全局命名空间污染 总是将代码包裹在一个立即的函数表达式里面,形成一个独立的模块. 不推荐 , y = ; c ...
- Python代码书写规范
Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不要使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在 ...
- c: c代码书写规范
排版: 较长的语句或函数过程参数(>80字符)要分成多行书写, 长表达式要在低优先级操作符处划分新行,操作符放在新行之首, 划分出的新行要进行适当的缩进,使排版整齐,语句可读 参考: 1. 运算 ...
随机推荐
- Hadoop面试题
1.把数据仓库从传统关系数据库转到hadoop有什么优势? 原关系存储方式昂贵 空间有限 hadoop支持结构化(例如 RDBMS),非结构化(例如 images,PDF,docs )和半结构化(例如 ...
- odoo开发笔记 -- div标签代替odoo button写法
odoo开发笔记 -- div标签代替odoo button写法 并调用自定义js <footer> <div id="confirm_request_cloud_repo ...
- 打成Jar包后运行报错 Unable to locate Spring NamespaceHandler for XML schema namespace
MAVEN项目,在IDEA中运行正常,但是把它打成jar包后再运行就会出现异常: Exception in thread "main" org.springframework. ...
- 项目ITP(五) spring4.0 整合 Quartz 实现任务调度
前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...
- java.sql.SQLException: The SQL statement must not be null or empty.这个错误
今天发现了这个错误 java.sql.SQLException: The SQL statement must not be null or empty. 并且看了些网页:综合说下这个错误. 一般都是 ...
- iOS逆向开发(0):修改二进制代码与重签名 | hopper | codesigh
小白:小程,你知道有些iOS程序是没人性的吗?老是不按我的意愿来运行! 小程:我怎么知道你的意愿就是有人性的? 本文解决一个问题:修改别人的二进制程序并运行起来. 让别人的程序按你的意愿来运行,文明一 ...
- JavaScript和Ajax部分(5)
41. jQuery中的load方法一般怎么用的? 答:load方法一般在 载入远程HTML 代码并插入到DOM中的时候用通常用来从Web服务器上获取静态的数据文件. 如果要传递参数的话,可以使用$. ...
- Java 类的加载机制
1.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构 ...
- Deploying Keras model on Tensorflow Serving--
keras训练了个二分类的模型.需求是把keras模型跑到 tensorflow serving上 (TensorFlow Serving 系统用于在生产环境中运行模型) keras模型转 tenso ...
- 【Flask-RESTPlus系列】Part2:响应编组
0x00 内容概览 响应编组 基本使用 重命名属性 默认值 自定义字段及多值情况 Url及其他具体字段 复杂结构 列表字段 嵌套字段 api.model()工厂 clone实现复制 api.inher ...