* 翻译 * From ribot
1. Android项目指南
1.1 工程结构
参考Android构建标准结构
1.2 文件命名
1.2.1 Class files类文件
类名遵循UpperCamelCase.
对与继承Android组件的类,类名应该以组件结尾。例如: SignInActivity
, SignInFragment
, ImageUploaderService
, ChangePasswordDialog
.
1.2.2 Resources files资源文件
资源文件以小写+下划线组合进行命名
1.2.2.1 Drawable files可绘制文件
可绘制文件的命名协定:
资源类型 | 前缀 | 举例 |
---|---|---|
Action bar | ab_ | ab_stacked.9.png |
Button | btn_ | btn_send_pressed.9.png |
Dialog | dialog_ | dialog_top.9.png |
Divider | divider_ | divider_horizontal.9.png |
Icon | ic_ | ic_star.png |
Menu | menu_ | menu_submenu_bg.9.png |
Notification | notification_ | notification_bg.9.png |
Tabs | tab_ | tab_pressed.9.png |
图标的命名协定:
资源类型 | 前缀 | 举例 |
---|---|---|
Icons | ic_ | ic_star.png |
Launcher icons | ic_launcher | ic_launcher_calendar.png |
Menu icons and Action Bar icons | ic_menu | ic_menu_archive.png |
Status bar icons | ic_stat_notify | ic_stat_notify_msg.png |
Tab icons | ic_tab | ic_tab_recent.png |
Dialog icons | ic_dialog | ic_dialog_info.png |
选择器状态命名协定:
状态 | 后缀 | 举例 |
---|---|---|
Normal正常 | _normal | btn_order_normal.9.png |
Pressed按下 | _pressed | btn_order_pressed.9.png |
Focused获取到焦点 | _focused | btn_order_focused.9.png |
Disabled不可用 | _disabled | btn_order_disabled.9.png |
Selected选中 | _selected | btn_order_selected.9.png |
1.2.2.2 Layout files布局文件
布局文件应该匹配安卓组件,把对应顶级的组件名放在最前面。例如,我们需要给 SignInActivity
创建布局文件,那么命名应该是activity_sign_in.xml
.
Component组件 | Class Name类名 | Layout Name布局名称 |
---|---|---|
Activity | UserProfileActivity | activity_user_profile.xml |
Fragment | SignUpFragment | fragment_sign_up.xml |
Dialog | ChangePasswordDialog | dialog_change_password.xml |
AdapterView item | item_person.xml | |
Partial局部layout | partial_stats_bar.xml |
当我们创建一个由Adapter
产生的布局文件,例如去构建一个ListView
。 在这种情况下,我们命名布局文件就要以item_
开头。
注意,有些情况下这些规则的不太适用。比如,当创建用于其他布局的局部布局文件,在这种情况下,我们命名布局文件就要以partial_
开头。
1.2.2.3 Menu files菜单文件
跟布局文件类似,菜单文件应该匹配组件名称。比如,如果我们定义一个在UserActivity
菜单文件,那么我们应该命名为activity_user
。
比较好的做法是命名中不包括menu
字样,因这些文件已经存放在menu
目录下面。
1.2.2.4 Values files 值文件
在值文件夹下的资源文件应该是 复数,例如strings.xml
, styles.xml
, colors.xml
, dimens.xml
, attrs.xml
License
Copyright 2015 Ribot Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.