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. 运算 ...
随机推荐
- Vue 加载外部js文件
Vue.js 加载外部js文件 在项目内新建一个config.js //变量的定义 export var config = { baseurl:'http://172.16.114.5:8088/M ...
- Xamarin.Android ImageView 图片圆角显示
第一步:在 values 文件夹下新增 Attrs.xml 文件 <?xml version="1.0" encoding="utf-8" ?> & ...
- .Net #if DEBUG调试模式代码使用
#if DEBUG Console.WriteLine("DEBUG:11111111111"); #else Console.WriteLine(" ...
- ionic 热更新 cordova-hot-code-push
cordova-hot-code-push ,Cordova热代码推送插件提供了在应用程序中执行基于Web的内容的自动更新的功能.使用此插件可以更新存储在项目的www文件夹中的所有内容. cordov ...
- linux取IP的几个方法
ifconfig eth0|grep " inet add"|cut -d":" -f2|cut -d " " -f1 ifconfig e ...
- Linux常用命令之文件搜索命令
目录 1.最强大的搜索命令:find2.在文件资料库中查找文件命令:locate 一.根据 文件或目录名称 搜索 二.根据 文件大小 搜索 三.根据 所有者和所属组 搜索 四.根据 时间属性 搜索 五 ...
- 【Go】strings.Replace 与 bytes.Replace 调优
原文链接:https://blog.thinkeridea.com/201902/go/replcae_you_hua.html 标准库中函数大多数情况下更通用,性能并非最好的,还是不能过于迷信标准库 ...
- 第3章 Linux上文件的权限管理
3.1 文件/目录的权限 3.1.1 文件的权限 每个文件都有其所有者(u:user).所属组(g:group)和其他人(o:other)对它的操作权限,a:all则同时代表这3者.权限包括读(r:r ...
- 第12章 Linux配置定时任务详解
12.1 配置定时任务 首先需弄清的概念: (1).crond是一个daemon类程序,路径为/usr/sbin/crond.默认会以后台方式启动,service或systemd方式启动crond默认 ...
- Tomcat(三):tomcat处理连接的详细过程
Tomcat系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html tomcat可以处理静态资源的请求,也可以通过servlet处理动态资源的请求 ...