原文链接 50 Android Studio Tips, Tricks & Resources you should be familiar with, as an Android Developer。
目录
本文的目的是列出 Android Studio 的有用提示、技巧、快捷键和资源,这些将提高您的整体效率和表现。
显然还有更多的优化、快捷键等,但是为了保持文章简洁,我把它限制在了 50 点。
希望你会喜欢它!
视觉
1. Android Logcat 的 Material Colors 主题
要更改 Android Studio Logcat,您需要转到: Preferences
(Windows / Linux
计算机上的 Settings
)→ Editor → Color Scheme → Android Logcat
并更改每种类型日志的 foreground 色。 您还需要取消选中一个选项 Inherit values from: Console -> Error output
。
我的 material colors:
- Assert
#BA68C8
- Debug
#2196F3
- Error
#F44336
- Info
#4CAF50
- Verbose
#BBBBBB
- Warning
#FF9800
2. 防止 Android Studio Logcat 在当前应用程序崩溃时清除日志。
为此,您需要转到 Android Monitor
面板,然后在右侧下拉列表中选择 Edit filter configuration
。
3. 将合适的代码样式应用于你的 IDE(IntelliJ / Android Studio)。
转到 Preferences → Code Style → Java
,在 Scheme
下拉列表中,您可以选择代码样式(或设置新的代码样式)。
2 种样式特别值得一提:
- Square Java Code Styles with Android
- Google Java Code Style
您可以导入主题,如下面的 gif 所示:
如果您使用 Kotlin,我建议您使用 Predefined Style 选项中的 Kotlin style guide。 要更改它,请转到:Code Style -> Kotlin -> Set from… -> Predefined Style -> Kotlin style guide
4. 使用分屏来提高效率。
要打开此功能,您需要在主屏幕的选项卡上单击鼠标右键,然后选择 Split Vertically / Horizontally
功能。
但为了尽可能提高效率,我们需要设置自定义的键盘快捷方式。 为此,请转到 Preferences → Keymap
并搜索 Split Vertically
。 然后打开上下文菜单,单击 Add Keyboard Shortcut
。
就我而言,对于垂直分割视图,我添加了 control + alt + v
。
它显示在下面的 gif 上。您也可以定义水平拆分视图的快捷方式。
5. Distraction Free Mode(无干扰模式)您可以通过以下方式启用它:View → Enter Distraction Free Mode
在无干扰模式下,编辑器占用整个 IntelliJ IDEA 框架,没有任何编辑器选项卡和工具窗口按钮。代码是中心对齐的。 [IntelliJ Idea Viewing Modes]
6. 使用 Live Templates
- 你能使用快捷键:
cmd + j
(Windows / Linux:ctrl + j
).
- 您可以使用许多已定义的模板,例如
Toasts
或if
条件。
- 您可以使用自己的自定义模板。 这是 Reto Meier 写的一篇很好的参考文章。 您还可以参考 IntelliJ IDEA documentation。
快捷键和有用的命令
1. 最好和最有用的命令是 Search for command
:cmd + shift + a
(Windows / Linux: ctrl + shift + a
).
假设您想关闭当前选项卡,但不知道如何关闭。你只需输入:close
,就能得到一个正确的快捷键/命令。
2. 从上次 复制/粘贴 中选择(管理剪贴板):cmd + shift + v
(Windows / Linux: ctrl + shift + v
).
默认情况下,有 5 个最后复制/粘贴的条目。
剪贴板堆栈的深度在 Settings 对话框的 Editor 页面的 Limits 部分中配置。 超过指定的数量时,将从列表中删除最旧的条目。 [Cutting, Copying and Pasting in IntelliJ IDEA]
3. 启用多光标功能: control + g
(alt + j
for Windows / Linux).
Bartek Lipinski 在 Medium上提供了有关此功能的详细文章。 强烈推荐!
4. 打开一个类: cmd + o
(Windows / Linux: ctrl + n
).
5. 打开任何文件: cmd + shift + o
(Windows / Linux: ctrl + shift + n
).
6. 打开符号: cmd + option + o
(Windows / Linux: alt + shift + n
).
7. 转到实现: cmd + option + b
(Windows / Linux: ctrl + alt + b
).
假设你有一个接口。通过单击接口的名称,然后按下 转到实现 快捷键,您将被重定向到实现该接口的类。
8. 转到声明: cmd + b
(Windows / Linux: ctrl + b
).
它允许您快速查看并转到类、方法或变量的声明。
9. 转到类型声明: control + shift + b
(Windows / Linux: ctrl + shift + b
).
假设你定义了:
Employee employee = new Employee(“Michal”);
当您的插入符号位于 employee
上并按下该快捷键时,您将被重定向到 Employee
类。
10. 转到super: cmd + u
(Windows / Linux: ctrl + u
).
例如,您重写了一些方法。当您的光标位于方法名上并单击 转到super 快捷键时,您将被重定向到父级方法。
11. 在选项卡之间移动: cmd + shift + [
(向左移动) 或者 cmd + shift + ]
(向右移动) (Windows / Linux: alt + ← / →
).
12. 在 layout 视图中的 Design / Text 选项卡之间移动: control + shift + ← / →
(Windows / Linux: alt + shift + ← / →
).
13. 关闭当前选项卡: cmd + w
(Windows / Linux: ctrl + F4
).
14. 隐藏所有窗口: cmd + shift + F12
(Windows / Linux: ctrl + shift + F12
).
15. 最小化 Android Studio 实例: cmd + m
(Windows / Linux: ctrl + m
).
16. 格式化你的代码: cmd + option + l
(Windows / Linux: ctrl + alt + l
).
17. 自动缩进行: control + option + i
(Windows / Linux: ctrl + alt + i
).
18. 实现方法: control + i
(Windows / Linux: ctrl + i
).
假设您实现了一个接口。那么,只需按下此快捷键,就可以快速导入该接口提供的所有方法。
19. 智能代码补全 (按期望的类型过滤方法和变量列表): control + shift + space
(Windows / Linux: ctrl + shift + space
).
20. 查找: cmd + f
(Windows / Linux: ctrl + f
).
21. 查找和替换: cmd + r
(Windows / Linux: ctrl + r
).
22. 将硬编码字符串移动到 resources: option + return
(Windows / Linux: alt + enter
). 当光标位于文本上时,才可以使用这组快捷键。查看下面的 gif:
如果您全局使用该快捷键,IDE 将执行 Project 快速修复(显示意图操作和快速修复)。
23. Build 和 run: control + r
(Windows / Linux: shift + F10
).
插件
- Key promoter - 一个插件,可以帮助您更频繁地使用键盘。
- String Manipulation - 为文本操作提供动作的插件(例如,切换诸如 camelcase、连字符-小写等样式、大写文本等)。
- Android Material Design Icon Generator - 此插件可帮助您将 Material Design 图标设置到你的 Android项目中。 安装后,您可以使用快捷键:
cmd + shift + m
生成图标(默认情况下)。 - ButterKnifeZelezny - 简单的插件,允许一键创建 Butterknife 视图注入。
- IntelliJ/Android Studio Plugin for Android Parcelable boilerplate code generation - 一个根据类中的字段生成 Android Parcelable 实现的插件。
- ADB Idea - 一个帮助和自动化 adb 命令的插件:卸载,终止,启动,重启,清除应用程序并清除应用程序数据并重新启动它。
- Genymotion plugin - 此插件允许您从 Android Studio 创建和启动 Genymotion 虚拟设备。
- Android Methods Count - 一个解析您的 Android 库依赖项的插件,并且可以将方法数显示为一个便捷的提示。
- Git Flow Integration - 用于引入 Git Flow branching model 的插件。
- Builder plugin - 一个插件(来自 Square),可以为类生成一个静态嵌套的 Builder。
资源
- Android Studio Tips of the Day by Philippe Breault
- Keyboard Shortcuts Reference
- (About) 10 Things You (Probably) Didn’t Know You Could do in Android Studio by Reto Meier
- Mouseless Driven Development by Hadi Hariri
- Android Studio Like a Boss by Philippe Breault
- Android Studio for Experts (Android Dev Summit 2015)
- Android Studio Tips and Tricks by Michael Evans
- Meet Android Studio
- Android Studio - Tips and Tricks by Donn Felker
- Debug Your App with Android Studio
- What are some of your must-have plugins for - Reddit discussion