1. 程式人生 > >AS 代碼模板 文件模板 Templates

AS 代碼模板 文件模板 Templates

列表 定位在 在哪裏 src star 情況 mark ngs esp

修改 File and Code Templates

Settings –> Editor –>【File and Code Templates】
或者在右鍵new時選擇子菜單【Edite File Templates...】

添加、修改文件模板 Files

–> 【Files】,可用來修改、添加新建某類型文件(比如Class、Interface、C++)時的文件模板
此文件保存位置【C:\Users\Administrator\.AndroidStudio2.3\config\fileTemplates\internal】
以下為新建Class文件的模板:
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

#if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK}
#end
#parse("File Header.java")

#if (${VISIBILITY} == "PUBLIC")public #end #if (${ABSTRACT} == "TRUE")abstract #end #if (${FINAL} == "TRUE")final #end class ${NAME} #if (${SUPERCLASS} != "")extends ${SUPERCLASS} #end #if (${INTERFACES} != "")implements ${INTERFACES} #end {
}
註意:上面代碼中的【File Header.java】文件即為下面的文件頭
技術分享This is a built-in template used each time you create a new Java class, by selecting New | Java Class | Class from the popup menu in one of the project views. The template is editable. Along with Java expressions and comments, you can also use predefined variables (listed below) that will then be expanded like macros into the corresponding values. It is also possible to specify an
arbitrary number of custom variables in the format ${<VARIABLE_NAME>}. In this case, before the new file is created, you will be prompted with a dialog where you can define particular values for all custom variables. Using the #parse directive, you can include templates from the Includes tab, by specifying the full name of the desired template as a parameter in quotation marks. For example:
#parse("File Header.java")

這是每次創建新的Java類時使用的內置模板,方法是選擇New | Java類| 一個項目視圖中的彈出菜單中的類。該模板是可編輯的。 除了Java表達式和註釋外,您還可以使用預定義的變量(如下所示),然後將其擴展為宏類型到相應的值。 也可以以 $ {<VARIABLE_NAME>} 格式指定任意數量的自定義變量。 在這種情況下,在創建新文件之前,將出現一個對話框,您可以在其中定義所有自定義變量的特定值。 使用 #parse 指令,可以通過將引用中的參數指定為所需模板的全名,從 Includes 選項卡中包含模板。 例如:#parse("File Header.java")

添加、修改文件頭 Includes【重要】

–> 【Includes】,可添加、修改文件頭
此文件保存位置【C:\Users\Administrator\.AndroidStudio2.3\config\fileTemplates\includes】
/**
 * 作者:<a href="http://www.cnblogs.com/baiqiantao">白乾濤</a><p>
 * 創建時間:${DATE} ${HOUR}:${MINUTE} <p>
 * 描述:
 */
This is a built-in template 內置的模板. It contains a code fragment片段 that can be included into file templates (Templates tab) with the help of the #parse directive指令. The template is editable. Along with除了 static text, code and comments註釋, you can also use predefined預定義的 variables變量 that will then be expanded like macros宏 into the corresponding相應的 values.

預定義的變量

Predefined variables will take the following values:
  • ${PACKAGE_NAME} //name of the package in which the new file is created
  • ${USER} //current user system login name
  • ${DATE} //current system date
  • ${TIME} //current system time
  • ${YEAR} //current year
  • ${MONTH} //current month
  • ${MONTH_NAME_SHORT} //first 3 letters of the current month name. Example: Jan, Feb, etc.
  • ${MONTH_NAME_FULL} //full name of the current month. Example: January, February, etc.
  • ${DAY} //current day of the month
  • ${HOUR} //current hour
  • ${MINUTE} //current minute
  • ${PROJECT_NAME} //the name of the current project
例如:【${DATE} ${HOUR}:${MINUTE}】的結果為【2017/9/20 10:28】

添加代碼模板 Live Templates【重要】

Setting->Editor->【Live Templates】
此文件保存位置【C:\Users\Administrator\.AndroidStudio2.3\config\templates】目錄下,文件名和你在AS中設置的名字一樣,但是會忽略掉名字中的中文。

【添加步驟】
1、Setting->Editor->【Live Templates】->點擊+,選擇【Template Group】創建一個自定義模板組
2、選中剛剛創建的group,點擊+,選擇【Live Template】創建一個自定義模板
  • Abbreviation:表示這個模板的快捷方式,你敲這些【字符+回車】後,模板就自動輸出了
  • Description:表示這個模板描述
  • Template text:模板的內容
3、點擊下面藍色的字"change",設置你這個快捷鍵在哪裏生效,一般把java或XML勾上就行了
4、點擊右側的按鈕"Edit variables",編輯模板信息中的變量
如我的Template text中定義了四個變量 bqt、date、name、bqt2,變量名字可以隨意定義(只能是字母或數字)
技術分享
  • bqt:可將鼠標定位在此位置
  • date、name:點擊下三角【選擇】合適的方法,可自動生成日期和類名
  • skip if defined:表示當自動生成的值不為空時,光標不停留在此方法處。建議選中
5、如上例,當我輸入【快捷鍵+回車】後輸出的內容為:
技術分享
  • 焦點首先在定位的第一個方法處,即bqt處;當我輸入內容後按下回車,或不輸入內容直按回車
  • 焦點轉移到下一個定義的方法處,即date處;當按下回車
  • 這時焦點應該會轉移到name處,但因為勾選了skip if defined,且此處有自動生成內容,所以焦點移到下一方法處
  • 即轉移到bqt2處,此時我再按下回車鍵,則焦點轉移到整個模板的最後(不會添加換行符)
2017-9-21


來自為知筆記(Wiz)

附件列表

    AS 代碼模板 文件模板 Templates