50 个安卓开发者应该熟悉的 android studio 技巧和资源

11,823 阅读6分钟
原文链接: www.jcodecraeer.com

编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识、前端、后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过!

.runtimead{ text-align:center ;background: radial-gradient(ellipse closest-side at center, rgba(255,255,255,1) 70%,rgba(251,252,253,1) 85%,rgba(244,247,250,1) 100%); padding: 25px;}

原文:50 Android Studio Tips, Tricks & Resources you should be familiar with, as an Android Developer 

本文列举出一些可以提高整体效率的Android Studio技巧和资源。

显然有太多太多这方面的东西,但是为了文章的简洁性,我把它限制在50个以内,希望你能喜欢!

视觉效果

1. Android Logcat的Material颜色主题。

1-90Z9yk4rWds8CupDwCo8Rw.png

要改变Android Studio的Logcat你需要这样做:进入Preferences (Windows上是Settings / Linux machines) → Editor → Colors & Fonts → Android Logcat,然后为每种类型的log设置前景颜色(foreground)。

我使用的material颜色:

  • Assert #BA68C8

  • Debug #2196F3

  • Error #F44336

  • Info #4CAF50

  • Verbose #BBBBBB

  • Warning #FF9800

注意里面有几个现有的主题,可以直接修改现有主题(不建议),或者点击save  as按钮拷贝一个主题并改名为Material theme Color然后再改变每种类型log的颜色。

2. 防止当前应用崩溃时Logcat清除log。

在Android Monitor面板的右上方点击下拉菜单中的choose Edit filter configuration:

1-fvHHZ6GNu5nuNQooAi99qQ.gif

注意是在工程界面的底部Android Monitor面板,而不是单独的Android Monitor工具中。

3. 使用一个适合自己的代码主题 (IntelliJ / Android Studio).

找到Preferences → Code Style → Java,在Scheme下拉菜单中你可以选择一个代码风格(或者设置一个新的)。

有两个风格值得专门提一下:

你可以按照下面gif图中的做法导入主题:

1-rDEZ3MCTBMuwFu8ZY6h3Rw.gif

4. 使用分屏提高效率

1-Lxh69qm-4ZZ4KAVAclItbA.png

要打开这个功能,右键主屏幕的选项卡(tab)选择 Split Vertically / Horizontally。

1-mDHTCaiZYCrMQa1Bry8bIQ (1).png

但是为了尽量提高效率我们需要设置一个自定义的快捷方式。为此找到到Preferences → Keymap,搜索Split Vertically。然后打开一个上下文菜单,点击Add Keyboard Shortcut。

我的分屏(vertical)快捷键设置的是control + alt + v。做法如下面的gif图所示。同样的你还可以为 horizontal split设置快捷键。

Untitled.gif

5. 无干扰模式

你可以到View → Enter Distraction Free Mode里启用它

1-RcKWZn7Ym0-L5qLQ64gbEQ.png

在无干扰模式下,编辑器占据了整个IntelliJ IDEA窗口,没有任何tab或者工具按钮。代码居中显示。[IntelliJ Idea Viewing Modes]

35612954-ED57-4039-9DC3-6B523C03002B.png

6. 使用Live Templates

  • 你可以使用快捷键:cmd + j (Windows / Linux: ctrl + j)。

    1-i9J_lzW8UpYboUojHoP-gA.gif

  • 可以使用已经定义好了的Live Templates,比如Toasts 或者if语句。

    1-vEU1cN9zT6sKDR5k_jJplg.gif

    1-XZoSS6csQ2OAhNrdpILgmw (1).gif

  • 可以使用自定义的templates。这里是Reto Meier的一篇不错的参考文章。你也可以参考IntelliJ IDEA的文档。

快捷键以及有用的命令

1.  最有用的命令是搜索命令的命令:cmd + shift + a (Windows / Linux: ctrl + shift + a)。

假设你想关闭当前tab却不知道怎么做,你只需输入close你就可以得到一个正确的快捷键/命令。

2. 选择最近的复制粘贴(管理剪切版):cmd + shift + v (Windows / Linux: ctrl + shift + v)。

默认有5个最近的复制/粘贴元素。

1-VGHdyecozNSSnrOZfaIiYQ.png

The depth of the Clipboard stack is configured in the Limits section on the Editor page of the Settings dialog box. When the specified number is exceeded, the oldest entry is removed from the list. [Cutting, Copying and Pasting in IntelliJ IDEA]

3. 启用多光标功能:control + g (alt + j for Windows / Linux)。

1-aE4ar0IEoPWuK5A_h2ir8g.gif

Bartek Lipinski在Medium上提供了一篇关于这个功能的详细文章。强烈推荐!

4. 打开一个类: cmd + o (Windows / Linux: ctrl + n)。

5. 打开任意文件: cmd + shift + o (Windows / Linux: ctrl + shift + n)。

6.打开symbol:cmd + option + o (Windows / Linux: alt + shift + n)。

7. 跳到实现:cmd + option + b (Windows / Linux: 
ctrl + alt + b)。

假如你有一个interface。通过点击接口的名字,然后按下快捷键就可以跳转到这个接口的实现。如果有多个实现会出现下拉选择。

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)。

比如,你重写了一些方法,当你的插入符号在方法名上,按下这个快捷键你将跳转到parent的这个方法。

11. Move between tabs: cmd + shift + [ (move left) or cmd + shift + ](move right) (Windows / Linux: alt + ← / →).

12. Move between Design / Text tabs in layout’s view: 
control + shift + ← / → (Windows / Linux: alt + shift + ← / →).

13. 关闭当前的tab:cmd + w (Windows / Linux: ctrl + shift + a)。

14. 隐藏所有窗口:cmd + shift + F12 (Windows / Linux: 
ctrl + shift + F12)。

15. 最小化 Android Studio instance: cmd + m (Windows / Linux: 
ctrl + m)。

16. 格式化代码: cmd + option + l (Windows / Linux: 
ctrl + alt + l)。

17. Auto-indent lines: control + option + i (Windows / Linux: 
ctrl + alt + i).

18. 实现接口的方法: control + i (Windows / Linux: ctrl + i)。

假设你要实现一个接口。这个快捷键可以让你快速导入这个接口的所有方法。

19. Smart code completion (filters the list of methods and variables by expected type): control + shift + space (Windows / Linux: ctrl + shift + space).

20. 查找: cmd + f (Windows / Linux: ctrl + f)。

21. 查找并替换: cmd + r (Windows / Linux: ctrl + r)。

22. 把一个硬编码的字符串放到资源文件中:option + return (Windows / Linux: alt + enter)。光标必须在这个文字之上时才能使用这个快捷键。看下面的gif图: 

1-IJsZ53P8n9n-fO55C88-bg.gif

插件

  1. Key promoter - 快捷键提示插件 当你点击鼠标一个功能的时候,可以提示 你这个功能快捷键是什么 ,和这个按钮你的使用频率 

  2. String Manipulation - a plugin which provides actions for text manipulation (e.g. toggling styles like camelCase, hyphen-lowercase etc., capitalize text and many more).

  3. Android Material Design Icon Generator -这个插件帮助你为工程添加Material Design图标。安装之后使用快捷键cmd + shift + m生成一个图标。

  4. ButterKnifeZelezny - 一键从 布局文件中 生成对于的 View 声明和 ButterKnife 注解:

    20160705154501976.gif

  5. IntelliJ/Android Studio Plugin for Android Parcelable boilerplate code generation - 一个根据类的成员生成Parcelable的插件。

  6. ADB Idea - 一个帮助你使用adb命令的插件:卸载,杀死,启动,重启,清除数据等命令。

  7. Genymotion plugin - 一个让你可以在Android Studio中创建和开启Genymotion虚拟设备的插件。

  8. Android Methods Count - a plugin that parses your Android library dependencies and shows the methods count as an handy hint.

  9. Git Flow Integration - a plugin for introducing a Git Flow branching model.

  10. Builder plugin - a plugin (from Square) that generates a static nested Builder for a class.


资源

  1. Android Studio Tips of the Day by Philippe Breault

  2. 快捷键参考

  3. (About) 10 Things You (Probably) Didn’t Know You Could do in Android Studio by Reto Meier。中文版:10个你可能不知道的 Android Studio技巧。 

  4. 无鼠标开发 by Hadi Hariri

  5. Android Studio Like a Boss by Philippe Breault

  6. Android Studio for Experts (Android Dev Summit 2015)

  7. Android Studio Tips and Tricks by Michael Evans

  8. 认识 Android Studio

  9. Android Studio - Tips and Tricks by Donn Felker

  10. Debug Your App with Android Studio

  11. What are some of your must-have plugins for - Reddit discussion

这就是本文的内容,请告诉我你知道的技巧和资源。

本文最初发表在我的个人博客