码迷,mamicode.com
首页 > 移动开发 > 详细

Android代码书写规范

时间:2016-11-23 23:10:08      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:devices   tab   title   any   new   rally   following   https   anim   

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代码书写规范

标签:devices   tab   title   any   new   rally   following   https   anim   

原文地址:http://www.cnblogs.com/alexjie-123/p/6095534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!