Android開發(fā)人員必知的 50 個秘訣、技巧和資源
作者撰寫本文的初衷,是為了羅列出Android Studio有用的提示、技巧、快捷方式和參考資源,將提高您的整體效率和操作性能。
顯然,還有很多優(yōu)化、快捷方式等,但作者為了保持這篇文章的簡短,限制為50個,希望讀者會喜歡這篇文章!
注:本文是以MacOS X操作Android Studio為例,與Windows / Linux不一致的地方已經(jīng)標注,請讀者注意這一點。
可視化
1.Android Logcat的材料顏色主題(Material Colors theme)。
要更改Android Studio Logcat,您需要依次點擊: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)用程序的日志。
要做到這一點,您需要到Android Monitor面板在右側(cè)下拉列表中選擇Edit filter configuration。
3. 將您的IDE(IntelliJ / Android Studio)應(yīng)用正確的代碼樣式。
依次點擊Preferences → Code Style → Java,在Scheme下拉列表中選擇您的代碼樣式(或設(shè)置一個新的代碼樣式)。
有2款風(fēng)格特別值得一提:
- Square Java Code Styles with Android
- Google Java Code Style
您可以導(dǎo)入主題,如下面的gif所示:
4. 使用分屏可提高效率。
要打開此功能,您需要在主屏幕的選項卡上單擊鼠標右鍵,然后選擇Split Vertically / Horizontally 。
但為了盡可能高效,我們需要設(shè)置一個自定義鍵盤的快捷方式。要做到這一點,依次點擊Preferences → Keymap 并找到Split Vertically。然后打開上下文菜單并單擊 Add Keyboard Shortcut。
以我為例,對于垂直拆分視圖,我增加了control + alt + v。 如下面gif圖所示。您也可以為水平分割視圖定義快捷方式。
5.無干擾模式(Distraction Free Mode)
您可以依次點擊: View → Enter Distraction Free Mode來開啟無干擾模式。
在無干擾模式下,編輯器占用整個IntelliJ IDEA框架,而沒有任何編輯器選項卡和工具按鈕,代碼按中心對齊。[IntelliJ Idea查看模式]
6. 使用活動模板
您可以使用快捷方式:cmd + j(Windows / Linux:ctrl + j)。
您可以使用許多已定義的模板,像Toasts 或if條件。
您可以使用自己的自定義模板。這里是Reto Meier的一篇參考文章。您還可以參考IntelliJ IDEA文檔。
快捷方式和有用的命令
1.***的、最有用的命令是Search for command:cmd + shift + a (Windows / Linux:ctrl + shift + a)。
當(dāng)你想關(guān)閉當(dāng)前選項卡,不知道該怎么辦時,您只需鍵入:close你會得到一個正確的快捷方式/命令。
2.從最近的復(fù)制/粘貼中選擇(管理剪貼簿):
cmd + shift + v(Windows / Linux:ctrl + shift + v)。
默認情況下,***有5個復(fù)制/粘貼項目。
剪貼板堆棧的深度在“設(shè)置”對話框的“編輯器”頁面的“限制”部分中配置。超過指定的數(shù)量時,將從列表中刪除最早的條目。 [在IntelliJ IDEA中剪切,復(fù)制和粘貼]
3.啟用多光標(multicursor)功能:control + g(Windows / Linux:alt + j)。
有關(guān)此功能的詳細文章由Bartek Lipinski在Medium網(wǎng)站提供。強烈推薦!
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è)您有一個界面,通過單擊接口的名稱,然后單擊Go to implementation的快捷方式,您將被重定向到實現(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)你的插入符號employee并單擊快捷方式時,將會被重定向到Employee類。
10.Go to super: cmd + u(Windows / Linux:ctrl + u)。
例如,您重寫一些方法。當(dāng)你在方法名稱插入符號,并單擊轉(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è)您實現(xiàn)了一個接口,然后只需單擊此快捷方式,即可快速導(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)在文本上使用插入符號時,必須使用快捷方式。檢查下面的gif:
如果您在全局使用該快捷方式,IDE將執(zhí)行Project快速修復(fù)(Project quick fix)(顯示意圖操作和快速修復(fù))。
23.Build and run: control + r (Windows / Linux: shift + F10)。
插件
Key promoter——幫助您更頻繁地使用鍵盤的插件。
String Manipulation——一個為文本操作提供操作的插件(例如,切換樣式,如camelCase、連字符-小寫等、大寫文本等還有很多)。
Android Material Design Icon Generator——此插件可幫助您將Material Design圖標設(shè)置為Android項目。安裝后,您可以使用快捷方式:cmd + shift + m以生成圖標(默認)。
ButterKnifeZelezny——一個簡單的插件,允許一鍵點擊創(chuàng)建Butterknife視圖注入。
IntelliJ/Android Studio Plugin for Android Parcelable boilerplate code generation——一個基于類中的字段生成Android Parcelable實現(xiàn)的插件。
ADB Idea——一個幫助和自動執(zhí)行adb命令的插件:卸載、殺死、啟動、重新啟動、清除應(yīng)用程序和清除應(yīng)用程序數(shù)據(jù)并重新啟動它。
Genymotion plugin——此插件允許您從Android Studio創(chuàng)建和啟動genymotion虛擬設(shè)備。
Android Methods Count——一個解析您的Android庫所依賴的插件,并顯示方法計數(shù)作為方便的提示。
Git Flow Integration——一個用于引入Git Flow分支模型的插件。
Builder plugin——一個插件(從Square)生成一個類的靜態(tài)嵌套Builder。
資源
Philippe Breault:《當(dāng)代Android Studio秘訣》
《鍵盤快捷方式參考》
Reto Meier:《在Android Studio中你可能不知道能做的十件事》
Hadi Hariri:《無鼠標驅(qū)策開發(fā)》
Philippe Breault:《像老板一樣的Android Studio》
《Android Studio 專家 (Android 開發(fā)峰會 2015)》
Michael Evans:《Android Studio 秘訣和技巧》
《遇見Android Studio》
Donn Felker:《Android Studio——秘訣和技巧》
《用Android Studio調(diào)試你的App》
《你應(yīng)該必備的插件——Reddit discussion》