50個(gè)Android Studio秘訣、技巧和資源
版權(quán)聲明
作者:Michal Bialas
譯者:劉志勇
本文由作者授權(quán)翻譯并發(fā)布,未經(jīng)許可不可轉(zhuǎn)載。
原文地址:https://medium.com/@mmbialas/50-android-studio-tips-tricks-resources-you-should-be-familiar-with-as-an-android-developer-af86e7cf56d2#.badcf6khh
作者撰寫本文的初衷,是為了羅列出Android Studio有用的提示、技巧、快捷方式和參考資源,將提高您的整體效率和操作性能。
顯然,Android Studio的使用技巧還有很多,還有很多優(yōu)化、快捷方式等,但作者為了保持這篇文章的簡(jiǎn)短,限制為50個(gè),希望讀者會(huì)喜歡這篇文章!
可視化
1. Android Logcat的輸出信息顏色主題(Material Colors theme)。
要更改Android Studio Logcat,您需要依次點(diǎn)擊:Preferences → Editor → Colors & Fonts → Android Logcat,然后更改每種類型日志的前景顏色。(注意,此處Perferences在Windows / Linux 是Settings。)
我的信息顏色如下:
- Assert #BA68C8
- Debug #2196F3
- Error #F44336
- Info #4CAF50
- Verbose #BBBBBB
- Warning #FF9800
2. 防止Android Studio Logcat崩潰,應(yīng)清除當(dāng)前應(yīng)用程序的日志。
要做到這一點(diǎn),您需要到Android Monitor面板在右側(cè)下拉列表中選擇Edit filter configuration。
3. 將您的IDE(IntelliJ / Android Studio)應(yīng)用正確的代碼樣式。
依次點(diǎn)擊Preferences → Code Style → Java,在Scheme下拉列表中選擇您的代碼樣式(或設(shè)置一個(gè)新的代碼樣式)。
有2款風(fēng)格特別值得一提:
- Square Java Code Styles with Android
- Google Java Code Style
您可以導(dǎo)入主題,如下面的gif所示:
4. 使用分屏可提高效率。
要打開此功能,您需要在主屏幕的選項(xiàng)卡上單擊鼠標(biāo)右鍵,然后選擇Split Vertically / Horizontally 。
但為了盡可能高效,我們需要設(shè)置一個(gè)自定義鍵盤的快捷方式。要做到這一點(diǎn),依次點(diǎn)擊Preferences → Keymap 并找到Split Vertically。然后打開上下文菜單并單擊 Add Keyboard Shortcut。
以我為例,對(duì)于垂直拆分視圖,我增加了control + alt + v。 您也可以為水平分割視圖定義快捷方式。
5. 無干擾模式(Distraction Free Mode)
您可以依次點(diǎn)擊: View → Enter Distraction Free Mode來開啟無干擾模式。
在無干擾模式下,編輯器占用整個(gè)IntelliJ IDEA框架,而沒有任何編輯器選項(xiàng)卡和工具按鈕,代碼按中心對(duì)齊。 [IntelliJ Idea查看模式] https://www.jetbrains.com/help/idea/2016.2/intellij-idea-viewing-modes.html
6. 使用活動(dòng)模板
您可以使用快捷方式:cmd + j(Windows / Linux:ctrl + j)。
您可以使用許多已定義的模板,像Toasts 或if條件。
您可以使用自己的自定義模板。下面是Reto Meier的一篇參考文章。您還可以參考IntelliJ IDEA文檔。
標(biāo)題快捷方式和命令
1. 最好的、最有用的命令是Search for command:cmd + shift + a (Windows / Linux:ctrl + shift + a)。
當(dāng)你想關(guān)閉當(dāng)前選項(xiàng)卡,不知道該怎么辦時(shí),您只需鍵入:close你會(huì)得到一個(gè)正確的快捷方式/命令。
2. 從最近的復(fù)制/粘貼中選擇(管理剪貼簿):
cmd + shift + v(Windows / Linux:ctrl + shift + v)。
默認(rèn)情況下,最后有5個(gè)復(fù)制/粘貼項(xiàng)目。
剪貼板堆棧的深度在“設(shè)置”對(duì)話框的“編輯器”頁面的“限制”部分中配置。超過指定的數(shù)量時(shí),將從列表中刪除最早的條目。 [在IntelliJ IDEA中剪切,復(fù)制和粘貼] https://www.jetbrains.com/help/idea/2016.2/cutting-copying-and-pasting.html
3. 啟用多光標(biāo)(multicursor)功能:control + g(Windows / Linux:alt + j)。
有關(guān)此功能的詳細(xì)文章由Bartek Lipinski在Medium網(wǎng)站提供。強(qiáng)烈推薦:
https://android.jlelse.eu/ctrl-g-d94c88cd4475#.ed3x7e91n
4. Open a class: cmd + o (Windows / Linux:ctrl + n)。
5. Open any file:cmd + shift + o (Windows / Linux:ctrl + shift + n)。
6. Open symbol : cmd + option + o (Windows / Linux:alt + shift + n)。
7. Go to implementation: cmd + option + b (Windows / Linux:ctrl + alt + b)。
假設(shè)您有一個(gè)界面,通過單擊接口的名稱,然后單擊Go to implementation的快捷方式,您將被重定向到實(shí)現(xiàn)該接口的類。
8. Go to declaration: cmd + b (Windows / Linux:ctrl + b)。 它允許您快速檢查并轉(zhuǎn)到類、方法或變量的聲明。
9. Go to type declaration: control + shift + b(Windows / Linux:ctrl + shift + b)。
假設(shè)您如此定義:
Employee employee = new Employee(“Michal”);
當(dāng)你的插入符號(hào)employee并單擊快捷方式時(shí),將會(huì)被重定向到Employee類。
10. Go to super: cmd + u(Windows / Linux:ctrl + u)。
例如,您重寫一些方法。當(dāng)你在方法名稱插入符號(hào),并單擊轉(zhuǎn)到Go to super快捷方式,您將被重定向到父方法。
11. Move between tabs: cmd + shift + [ 或 cmd + shift + ] (Windows / Linux:alt + ← / →)。
12. Move between Design / Text tabs in layout’s view: control + shift + ← / →(Windows / Linux:alt + shift + ← / →)。
13. Close a current tab: cmd + w(Windows / Linux:ctrl + shift + a)。
14. Hide all windows: cmd + shift + F12 (Windows / Linux:ctrl + shift + F12)。
15. Minimize Android Studio instance: cmd + m(Windows / Linux:ctrl + m)。
16. Format your code: cmd + option + l (Windows / Linux:ctrl + alt + l)。
17. Auto-indent lines: control + option + i (Windows / Linux:ctrl + alt + i)。
18. Implement methods: control + i (Windows / Linux:ctrl + i)。
假設(shè)您實(shí)現(xiàn)了一個(gè)接口,然后只需單擊此快捷方式,即可快速導(dǎo)入該界面提供的所有方法。
19. Smart code completion(按期望類型過濾方法和變量的列表):control + shift + space (Windows / Linux:ctrl + shift + space)。
20. Find: cmd + f(Windows / Linux: ctrl + f)。
21. Find and replace:cmd + r(Windows / Linux: ctrl + r)。
22. Move hardcoded strings to resources: option + return (Windows / Linux: alt + enter)。
當(dāng)在文本上使用插入符號(hào)時(shí),必須使用快捷方式。檢查下面的gif:
如果您在全局使用該快捷方式,IDE將執(zhí)行Project快速修復(fù)(Project quick fix)(顯示意圖操作和快速修復(fù))。
23. Build and run: control + r (Windows / Linux: shift + F10)。
插件
1.Key promoter——幫助您更頻繁地使用鍵盤的插件:https://plugins.jetbrains.com/plugin/4455
2.String Manipulation——一個(gè)為文本操作提供操作的插件(例如,切換樣式,如camelCase、連字符-小寫等、大寫文本等還有很多):https://plugins.jetbrains.com/plugin/2162
3.Android Material Design Icon Generator——此插件可幫助您將Material Design圖標(biāo)設(shè)置為Android項(xiàng)目。安裝后,您可以使用快捷方式:cmd + shift + m以生成圖標(biāo)(默認(rèn)):https://github.com/konifar/android-material-design-icon-generator-plugin
4.ButterKnifeZelezny——一個(gè)簡(jiǎn)單的插件,允許一鍵點(diǎn)擊創(chuàng)建Butterknife視圖注入:https://github.com/avast/android-butterknife-zelezny
5.IntelliJ/Android Studio Plugin for Android Parcelable boilerplate code generation——一個(gè)基于類中的字段生成Android Parcelable實(shí)現(xiàn)的插件:https://github.com/mcharmas/android-parcelable-intellij-plugin
6.ADB Idea——一個(gè)幫助和自動(dòng)執(zhí)行adb命令的插件:卸載、殺死、啟動(dòng)、重新啟動(dòng)、清除應(yīng)用程序和清除應(yīng)用程序數(shù)據(jù)并重新啟動(dòng)它:https://github.com/pbreault/adb-idea
7.Genymotion plugin——此插件允許您從Android Studio創(chuàng)建和啟動(dòng)Genymotion虛擬設(shè)備:https://www.genymotion.com/plugins/
8.Android Methods Count——一個(gè)解析您的Android庫所依賴的插件,并顯示方法計(jì)數(shù)作為方便的提示:http://www.methodscount.com/plugins
9.Git Flow Integration——一個(gè)用于引入Git Flow分支模型的插件:https://plugins.jetbrains.com/plugin/7315
10.Builder plugin——一個(gè)插件(從Square)生成一個(gè)類的靜態(tài)嵌套Builder:https://plugins.jetbrains.com/plugin/7567
資源
1.Philippe Breault:《當(dāng)代Android Studio秘訣》 https://plus.google.com/collection/wtO0PB
2.《鍵盤快捷方式參考》https://developer.android.com/studio/intro/keyboard-shortcuts.html
3.Reto Meier:《在Android Studio中你可能不知道能做的十件事》 https://medium.com/google-developers/about-10-things-you-probably-didn-t-know-you-could-do-in-android-studio-de231071b375#.toxstt9em
4.Hadi Hariri:《無鼠標(biāo)驅(qū)策開發(fā)》 https://vimeo.com/98922030
5.Philippe Breault:《像老板一樣的Android Studio》 https://realm.io/news/360andev-philippe-breault-android-studio-ide-like-boss-structural-search-refactoring-java/
6.《Android Studio 專家 (Android 開發(fā)峰會(huì) 2015)》 https://www.youtube.com/watch?v=Y2GC6P5hPeA
7. Michael Evans:《Android Studio 秘訣和技巧》 http://michaelevans.org/blog/2016/01/06/android-studio-tips-and-tricks/
8.《遇見Android Studio》 https://developer.android.com/studio/intro/index.html
9. Donn Felker:《Android Studio——秘訣和技巧》 http://www.donnfelker.com/android-studio-tips-and-tricks-part-1/
10.《用Android Studio調(diào)試你的App》 https://developer.android.com/studio/debug/index.html
11.《你應(yīng)該必備的插件——Reddit discussion》 https://www.reddit.com/r/androiddev/comments/3ktqyb/what_are_some_of_your_musthave_plugins_for/