@zouzhenglu
2017-02-13T14:05:00.000000Z
字数 6921
阅读 135
AndroidStudio Live File Plugin Template
在日常开发中,适当的使用模板,可以提高开发速度,减少重复敲代码的时间,也避免了细节上的错误。一次编写,,到处使用。强大的模板,还可以帮组新同事快速进入开发状态。也就大大的提高了开发速度。
as内置了三种模板,这里,将简单的介绍下这几个模板
| 模板 | 适用范围 | 备注 |
|---|---|---|
| live templates | 代码块 | 小巧便捷 |
| file templates | 单文件创建 | 设置简单,使用也不算复杂 |
| plugin templates | 多文件创建 | 功能强大,定义模板比较麻烦 |
在代码中输入key得到相应提示的模板,一般用于简化一些常用的语句,常规性代码块。
系统内置了很多的模板,可以根据实际习惯修改,也可以添加新的模板。
比如,在方法中,输入sout,敲下enter或者tab就可以输出System.out.println(),
as支持设置变量,变量可以使用内置函数自动赋值,也可以手动敲入,
适用范围:代码块
设置入口:settings-->Editor-->Live Templates
在创建文件的时候,as提供了一些模板,如java、Singleton等,在创建后就自动生成模板的代码,一般用于单文件级别的代码模板,大多数人用他的header来修改注释模板吧,其实如果将整个模板代码写进去也是ok,
缺点:只能处理当前文件,
比如,你用这个模板设置了Activity的模板代码,最终还是要跑到manifest去注册这个activity
适用范围:单文件创建
设置入口:settings-->Editor-->File and Code Templates
在创建Activity的时候,你会发现,as会自动将你的activity加入manifest,创建相应的layout.xml,这个模板的强大在于,他可以在创建文件的时候,同时处理多个文件
缺点:定义比较麻烦,而且,每次升级as后,会自动还原模板,需要备份好模板,以便下次copy
使用范围:多文件创建
设置入口:%as安装根目录%\plugins\android\lib\templates\activities
这是本文要重点讲的一块,这个模板在as中只能没有开放设置,只能找到安装目录,将模板放进去
在activities中,我们能看到需要熟悉的文件名,这就是我们用as创建activity的时候内置的模板。
以EmptyActivity为例,需要用到common,其他的文件夹先忽略.
我们看到,这里是用了ftl,不会的话没关系,这个基本上会一门语言的人都能看懂
里面放的是代码的模板文件,XXX.java.ftl,XXX.xml.ftl
定义全局变量,引用了common的全局变量
<?xml version="1.0"?><globals><global id="hasNoActionBar" type="boolean" value="false" /><global id="parentActivityClass" value="" /><global id="simpleLayoutName" value="${layoutName}" /><global id="excludeMenu" type="boolean" value="true" /><global id="generateActivityTitle" type="boolean" value="false" /><#include "../common/common_globals.xml.ftl" /></globals>
定义了执行文件
recipe.xml.ftl<?xml version="1.0"?><recipe><#include "../common/recipe_manifest.xml.ftl" /><#if generateLayout><#include "../common/recipe_simple.xml.ftl" /><open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" /></#if><instantiate from="root/src/app_package/SimpleActivity.java.ftl"to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /><open file="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /></recipe>
上面的代码做了三件事
合并manifest
common/recipe_manifest.xml.ftl<recipe folder="root://activities/common"><merge from="root/AndroidManifest.xml.ftl"to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" /><merge from="root/res/values/manifest_strings.xml.ftl"to="${escapeXmlAttribute(resOut)}/values/strings.xml" /></recipe>
common/root/AndroidManifest.xml.ftl<manifest xmlns:android="http://schemas.android.com/apk/res/android" ><application><activity android:name="${relativePackage}.${activityClass}"<#if generateActivityTitle!true><#if isNewProject>android:label="@string/app_name"<#else>android:label="@string/title_${activityToLayout(activityClass)}"</#if></#if><#if hasNoActionBar>android:theme="@style/${themeNameNoActionBar}"</#if><#if buildApi gte 16 && parentActivityClass != "">android:parentActivityName="${parentActivityClass}"</#if>><#if parentActivityClass != ""><meta-data android:name="android.support.PARENT_ACTIVITY"android:value="${parentActivityClass}" /></#if><#if isLauncher && !(isLibraryProject!false)><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></#if></activity></application></manifest>
自动生成layout.xml布局文件
common/recipe_simple.xml.ftl......<instantiate from="root/res/layout/simple.xml.ftl"to="${escapeXmlAttribute(resOut)}/layout/${simpleLayoutName}.xml" />......
自动生成activity.java代码文件,并打开
recipe.xml.ftl......<instantiate from="root/src/app_package/SimpleActivity.java.ftl"to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" />......
用户创建界面,用户获取用户输入的数据
template.xml<?xml version="1.0"?><templateformat="5"revision="5"name="Empty Activity"minApi="9"minBuildApi="14"description="Creates a new empty activity"><category value="Activity" /><formfactor value="Mobile" /><parameterid="activityClass"name="Activity Name"type="string"constraints="class|unique|nonempty"suggest="${layoutToActivity(layoutName)}"default="MainActivity"help="The name of the activity class to create" /><parameterid="generateLayout"name="Generate Layout File"type="boolean"default="true"help="If true, a layout file will be generated" /><parameterid="layoutName"name="Layout Name"type="string"constraints="layout|unique|nonempty"suggest="${activityToLayout(activityClass)}"default="activity_main"visibility="generateLayout"help="The name of the layout to create for the activity" /><parameterid="isLauncher"name="Launcher Activity"type="boolean"default="false"help="If true, this activity will have a CATEGORY_LAUNCHER intent filter, making it visible in the launcher" /><parameterid="backwardsCompatibility"name="Backwards Compatibility (AppCompat)"type="boolean"default="true"help="If false, this activity base class will be Activity instead of AppCompatActivity" /><parameterid="packageName"name="Package name"type="string"constraints="package"default="com.mycompany.myapp" /><!-- 128x128 thumbnails relative to template.xml --><thumbs><!-- default thumbnail is required --><thumb>template_blank_activity.png</thumb></thumbs><globals file="globals.xml.ftl" /><execute file="recipe.xml.ftl" /></template>
上面做了几件事
模板类型
<category value="Activity" />
给模板归类,类名相同的模板在创建代码的时候会在同个目录下
如果有多个项目,每个项目都有各自的模板,这时候就可以用以区分了,避免混乱了,毕竟放在Activity下面,会有很多系统内置的干扰
用户输入
template.xml......<parameterid="activityClass"name="Activity Name"type="string"constraints="class|unique|nonempty"suggest="${layoutToActivity(layoutName)}"default="MainActivity"help="The name of the activity class to create" /><parameterid="generateLayout"name="Generate Layout File"type="boolean"default="true"help="If true, a layout file will be generated" /><parameterid="layoutName"name="Layout Name"type="string"constraints="layout|unique|nonempty"suggest="${activityToLayout(activityClass)}"default="activity_main"visibility="generateLayout"help="The name of the layout to create for the activity" />......
type 类型,string则是一个输入框,boolean则是一个checkbox
constraints是约束条件nonempty:不能空则不能继续unique:唯一,如果有重名文件存在,则会自动在后面加上数字标示,eg:Activity2.java
suggest自动完善,as会按照规则自动生成,用户不用因为有多个输入框而输入多个似是而非的数据eg:上面的layoutName 会在用户输入activityClass后自动生成,LoginActivity --> act
执行
template.xml......<globals file="globals.xml.ftl" /><execute file="recipe.xml.ftl" />......
很简单的两句话,使用定义好的全局变量文件,执行recpie.xml.ftl创建模板
在创建的时候显示的图片,无视他
package ${packageName};import ${superClassFqcn};import android.os.Bundle;......public class ${activityClass} extends ${superClass} {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);<#if generateLayout>setContentView(R.layout.${layoutName});</#if>......}
这是一个ftl的模板
支持变量表达式${activityClass}
支持if else ,,在if 语句中,直接使用变量activityClass,不需要${}
这里使用的activityClass,generateLayout,layoutName都是在template.xml中定义的
superClass,packagename,superClassFqcn则是在globals.xml.ftl定义的全局变量
具体可以根据实际需求定制template.xml获取想要的数据,模板代码完全是可以copy+改
对于ftl模板而言,并不区分你的模板代码是xml还是java。所以,如果你想创建layout的模板,也是一样的方式,这里不再多说。
plugin templates 模板虽然比起另外两种模板写起来麻烦很多,但他也同样的强大了许多。
另外,写模板的时候,需要重启as才能生效的,哪怕你是修改了一个字母,请重启生效,所以,建议在写模板的时候就新建一个空项目吧。
说了这么多废话,,下面进入正题