我們用Eclipse做Android開發(fā)的時候,可以導(dǎo)入Google提供的profile文件,這樣每次“Ctrl+Shift+F”的時候秀撇,IDE就能夠按照官方的規(guī)范來進行代碼格式化。
下載
Eclipse用的profile文件在GitHub上的這個位置:
https://github.com/android/platform_development/tree/master/ide/eclipse
Java用的profile文件有兩個:
- android.importorder:用來自動調(diào)整import語句的順序;
- android-formatting.xml:用來格式化代碼大溜。
下載鏈接:
- android.importorder:https://raw.github.com/android/platform_development/master/ide/eclipse/android.importorder
- android-formatting.xml:https://raw.github.com/android/platform_development/master/ide/eclipse/android-formatting.xml
導(dǎo)入
- Eclipse主菜單Window > Preferences,打開Preferences窗口估脆;
- 在“Java > Code Style > Formatter ”中導(dǎo)入android-formatting.xml钦奋;
- 在“Java > Code Style > Organize Imports ”中導(dǎo)入android.importorder。
導(dǎo)入profile
優(yōu)化設(shè)置
建議1:不限定每行注釋的最大長度
如果項目有在使用Javadoc疙赠,建議不要限定每行注釋的最大長度付材,以免因為自動換行而破壞注釋的排版。
設(shè)置步驟如下:
- 打開Profile‘Android’的編輯窗口圃阳;
-
在Comments頁厌衔,把Maximum line width for comments設(shè)置成999:
設(shè)置每行注釋的最大長度
建議2:不限定每行代碼的最大長度
不限定每行代碼的最大長度,以免因為自動換行而破壞代碼的排版捍岳。
設(shè)置步驟如下:
- 打開Profile‘Android’的編輯窗口富寿;
-
在Line Wrapping頁,把Maximum line width設(shè)置成999:
設(shè)置每行代碼的最大長度