[关闭]
@zouzhenglu 2017-02-13T14:05:00.000000Z 字数 6921 阅读 79

android studio 模板介绍

AndroidStudio Live File Plugin Template


前言

在日常开发中,适当的使用模板,可以提高开发速度,减少重复敲代码的时间,也避免了细节上的错误。一次编写,,到处使用。强大的模板,还可以帮组新同事快速进入开发状态。也就大大的提高了开发速度。
as内置了三种模板,这里,将简单的介绍下这几个模板

模板 适用范围 备注
live templates 代码块 小巧便捷
file templates 单文件创建 设置简单,使用也不算复杂
plugin templates 多文件创建 功能强大,定义模板比较麻烦

一、live templates

在代码中输入key得到相应提示的模板,一般用于简化一些常用的语句,常规性代码块。
系统内置了很多的模板,可以根据实际习惯修改,也可以添加新的模板。
比如,在方法中,输入sout,敲下enter或者tab就可以输出System.out.println()
as支持设置变量,变量可以使用内置函数自动赋值,也可以手动敲入,

适用范围:代码块
设置入口:settings-->Editor-->Live Templates

二、File templates

在创建文件的时候,as提供了一些模板,如java、Singleton等,在创建后就自动生成模板的代码,一般用于单文件级别的代码模板,大多数人用他的header来修改注释模板吧,其实如果将整个模板代码写进去也是ok,
缺点:只能处理当前文件,
比如,你用这个模板设置了Activity的模板代码,最终还是要跑到manifest去注册这个activity

适用范围:单文件创建
设置入口:settings-->Editor-->File and Code Templates

三、plugin templates

在创建Activity的时候,你会发现,as会自动将你的activity加入manifest,创建相应的layout.xml,这个模板的强大在于,他可以在创建文件的时候,同时处理多个文件
缺点:定义比较麻烦,而且,每次升级as后,会自动还原模板,需要备份好模板,以便下次copy

使用范围:多文件创建
设置入口:%as安装根目录%\plugins\android\lib\templates\activities

这是本文要重点讲的一块,这个模板在as中只能没有开放设置,只能找到安装目录,将模板放进去

四、深入了解系统内置模板

在activities中,我们能看到需要熟悉的文件名,这就是我们用as创建activity的时候内置的模板。
以EmptyActivity为例,需要用到common,其他的文件夹先忽略.

图片
我们看到,这里是用了ftl,不会的话没关系,这个基本上会一门语言的人都能看懂

1. root

里面放的是代码的模板文件,XXX.java.ftl,XXX.xml.ftl

2. globals.xml.ftl

定义全局变量,引用了common的全局变量

  1. <?xml version="1.0"?>
  2. <globals>
  3. <global id="hasNoActionBar" type="boolean" value="false" />
  4. <global id="parentActivityClass" value="" />
  5. <global id="simpleLayoutName" value="${layoutName}" />
  6. <global id="excludeMenu" type="boolean" value="true" />
  7. <global id="generateActivityTitle" type="boolean" value="false" />
  8. <#include "../common/common_globals.xml.ftl" />
  9. </globals>

3.recipe.xml.ftl

定义了执行文件

  1. recipe.xml.ftl
  2. <?xml version="1.0"?>
  3. <recipe>
  4. <#include "../common/recipe_manifest.xml.ftl" />
  5. <#if generateLayout>
  6. <#include "../common/recipe_simple.xml.ftl" />
  7. <open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
  8. </#if>
  9. <instantiate from="root/src/app_package/SimpleActivity.java.ftl"
  10. to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" />
  11. <open file="${escapeXmlAttribute(srcOut)}/${activityClass}.java" />
  12. </recipe>

上面的代码做了三件事

a. 合并manifest

b. 自动生成layout.xml布局文件,并打开

c. 自动生成activity.java代码文件,并打开

合并manifest

  1. common/recipe_manifest.xml.ftl
  2. <recipe folder="root://activities/common">
  3. <merge from="root/AndroidManifest.xml.ftl"
  4. to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" />
  5. <merge from="root/res/values/manifest_strings.xml.ftl"
  6. to="${escapeXmlAttribute(resOut)}/values/strings.xml" />
  7. </recipe>
  1. common/root/AndroidManifest.xml.ftl
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <application>
  4. <activity android:name="${relativePackage}.${activityClass}"
  5. <#if generateActivityTitle!true>
  6. <#if isNewProject>
  7. android:label="@string/app_name"
  8. <#else>
  9. android:label="@string/title_${activityToLayout(activityClass)}"
  10. </#if>
  11. </#if>
  12. <#if hasNoActionBar>
  13. android:theme="@style/${themeNameNoActionBar}"
  14. </#if>
  15. <#if buildApi gte 16 && parentActivityClass != "">
  16. android:parentActivityName="${parentActivityClass}"
  17. </#if>>
  18. <#if parentActivityClass != "">
  19. <meta-data android:name="android.support.PARENT_ACTIVITY"
  20. android:value="${parentActivityClass}" />
  21. </#if>
  22. <#if isLauncher && !(isLibraryProject!false)>
  23. <intent-filter>
  24. <action android:name="android.intent.action.MAIN" />
  25. <category android:name="android.intent.category.LAUNCHER" />
  26. </intent-filter>
  27. </#if>
  28. </activity>
  29. </application>
  30. </manifest>

自动生成layout.xml布局文件

  1. common/recipe_simple.xml.ftl
  2. ......
  3. <instantiate from="root/res/layout/simple.xml.ftl"
  4. to="${escapeXmlAttribute(resOut)}/layout/${simpleLayoutName}.xml" />
  5. ......

自动生成activity.java代码文件,并打开

  1. recipe.xml.ftl
  2. ......
  3. <instantiate from="root/src/app_package/SimpleActivity.java.ftl"
  4. to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" />
  5. ......

4.template.xml

用户创建界面,用户获取用户输入的数据

  1. template.xml
  2. <?xml version="1.0"?>
  3. <template
  4. format="5"
  5. revision="5"
  6. name="Empty Activity"
  7. minApi="9"
  8. minBuildApi="14"
  9. description="Creates a new empty activity">
  10. <category value="Activity" />
  11. <formfactor value="Mobile" />
  12. <parameter
  13. id="activityClass"
  14. name="Activity Name"
  15. type="string"
  16. constraints="class|unique|nonempty"
  17. suggest="${layoutToActivity(layoutName)}"
  18. default="MainActivity"
  19. help="The name of the activity class to create" />
  20. <parameter
  21. id="generateLayout"
  22. name="Generate Layout File"
  23. type="boolean"
  24. default="true"
  25. help="If true, a layout file will be generated" />
  26. <parameter
  27. id="layoutName"
  28. name="Layout Name"
  29. type="string"
  30. constraints="layout|unique|nonempty"
  31. suggest="${activityToLayout(activityClass)}"
  32. default="activity_main"
  33. visibility="generateLayout"
  34. help="The name of the layout to create for the activity" />
  35. <parameter
  36. id="isLauncher"
  37. name="Launcher Activity"
  38. type="boolean"
  39. default="false"
  40. help="If true, this activity will have a CATEGORY_LAUNCHER intent filter, making it visible in the launcher" />
  41. <parameter
  42. id="backwardsCompatibility"
  43. name="Backwards Compatibility (AppCompat)"
  44. type="boolean"
  45. default="true"
  46. help="If false, this activity base class will be Activity instead of AppCompatActivity" />
  47. <parameter
  48. id="packageName"
  49. name="Package name"
  50. type="string"
  51. constraints="package"
  52. default="com.mycompany.myapp" />
  53. <!-- 128x128 thumbnails relative to template.xml -->
  54. <thumbs>
  55. <!-- default thumbnail is required -->
  56. <thumb>template_blank_activity.png</thumb>
  57. </thumbs>
  58. <globals file="globals.xml.ftl" />
  59. <execute file="recipe.xml.ftl" />
  60. </template>

上面做了几件事

a. 模板类型

b. 用户输入

c. 执行

模板类型
<category value="Activity" />
给模板归类,类名相同的模板在创建代码的时候会在同个目录下
如果有多个项目,每个项目都有各自的模板,这时候就可以用以区分了,避免混乱了,毕竟放在Activity下面,会有很多系统内置的干扰

用户输入

  1. template.xml
  2. ......
  3. <parameter
  4. id="activityClass"
  5. name="Activity Name"
  6. type="string"
  7. constraints="class|unique|nonempty"
  8. suggest="${layoutToActivity(layoutName)}"
  9. default="MainActivity"
  10. help="The name of the activity class to create" />
  11. <parameter
  12. id="generateLayout"
  13. name="Generate Layout File"
  14. type="boolean"
  15. default="true"
  16. help="If true, a layout file will be generated" />
  17. <parameter
  18. id="layoutName"
  19. name="Layout Name"
  20. type="string"
  21. constraints="layout|unique|nonempty"
  22. suggest="${activityToLayout(activityClass)}"
  23. default="activity_main"
  24. visibility="generateLayout"
  25. help="The name of the layout to create for the activity" />
  26. ......
  1. type 类型,string则是一个输入框,boolean则是一个checkbox
  1. constraints是约束条件
  2. nonempty:不能空则不能继续
  3. unique:唯一,如果有重名文件存在,则会自动在后面加上数字标示,eg:Activity2.java
  1. suggest自动完善,as会按照规则自动生成,用户不用因为有多个输入框而输入多个似是而非的数据
  2. eg:上面的layoutName 会在用户输入activityClass后自动生成,LoginActivity --> act

执行

  1. template.xml
  2. ......
  3. <globals file="globals.xml.ftl" />
  4. <execute file="recipe.xml.ftl" />
  5. ......

很简单的两句话,使用定义好的全局变量文件,执行recpie.xml.ftl创建模板

5.xxx.png

在创建的时候显示的图片,无视他

5.代码模板 SimpleActivity.java.ftl

  1. package ${packageName};
  2. import ${superClassFqcn};
  3. import android.os.Bundle;
  4. ......
  5. public class ${activityClass} extends ${superClass} {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. <#if generateLayout>
  10. setContentView(R.layout.${layoutName});
  11. </#if>
  12. ......
  13. }

这是一个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才能生效的,哪怕你是修改了一个字母,请重启生效,所以,建议在写模板的时候就新建一个空项目吧。


说了这么多废话,,下面进入正题
福利

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注