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. 运算 ...
随机推荐
- python之找最后一个人
题目大概是:有10个人围成一圈,从第一个人数,数到3的人出局,问最后一个人是谁? 围成一圈,那就是无限循环,直至最后一个人,我们可以把10个人看做一个列表,每循环一次就把除3为0的数去除,下次再次循环 ...
- sql server 索引阐述系列七 索引填充因子与碎片
一.概述 索引填充因子作用:提供填充因子选项是为了优化索引数据存储和性能. 当创建或重新生成索引时,填充因子的值可确定每个叶级页上要填充数据的空间百分比,以便在每一页上保留一些剩余存储空间作为以后扩展 ...
- Thrown "KeeperErrorCode = Unimplemented for /services" exception
1.环境 spring-boot 2.1.3 依赖项:spring-cloud-starter-zookeeper-discovery 版本2.1.1 使用的zookeeper3.4.11 代码如下: ...
- Hibernate学习(七)———— hibernate中查询方式详解
序言 之前对hibernate中的查询总是搞混淆,不明白里面具体有哪些东西.就是因为缺少总结.在看这篇文章之前,你应该知道的是数据库的一些查询操作,多表查询等 --WH 一.hibernate中的5种 ...
- npm设置和查看仓库源
转载请注明出处:https://www.cnblogs.com/wenjunwei/p/10078460.html 在使用npm命令时,如果直接从国外的仓库下载依赖,下载速度很慢,甚至会下载不下来,我 ...
- Web前端课程设计——个人主页
大三上学期期末总结,嗯,没错,是上学期,写在新学期开始hhh. 上学期学了一门HTML5+CSS3的课程,也叫Web前端技术,期末的课程设计是写一个个人主页,能够在浏览器中打开的静态网页.通过一学期的 ...
- 海量数据处理之BitMap
有这样一种场景:一台普通PC,2G内存,要求处理一个包含40亿个不重复并且没有排过序的无符号的int整数,给出一个整数,问如果快速地判断这个整数是否在文件40亿个数据当中? 问题思考: 40亿个int ...
- WPF 窗口
在WPF中,经常需要对窗口进行设置,下面讲讲常用的几个设置. 窗口样式 1.无边框窗口 无边框透明窗体 设置 WindowStyle="None"--无边框,如果需要其它按钮,如缩 ...
- .net 公共基础类
using WL.Infrastructure.Http; using System; using System.Collections.Generic; using System.IO; using ...
- 数据库 'xxxx' 的事务日志已满。若要查明无法重用日志中的空间的原因
一.出现的背景: 在SQL server中执行SQL语句出现如下图: 二.出现的原因: 我到数据库的服务器看了一下硬盘空间发现此数据库所在的D盘空间几乎已经用尽.如图: 三.解决方法: 第一种方法:直 ...