本篇文章主要介绍 Android Studio 代码模版,通过使用代码模版让我们更加方便的去创建 MVP 类。其他类:Adapter、List 等同样适用。
先上一波效果图
使用时和正常的 new Activity 一样。
new Activity 后生成的类:
详细的 Activity 类:
详细的 Presenter 类:
详细的 Repository 类:
只需要我正常的一个 new Activity 的流程,就可以自动生成这些类,而且还可以自定义,是不是很方便,很快捷?麻麻再也不用担心 MVP 框架中创建一大堆类头疼的问题了。
代码模版的相关知识
-
找到 Android Studio 代码模版的文件位置。
文件位置:
Android Studio 的安装目录\plugins\android\lib\templates\activities
例:
D:\Android\studio3\android-studio\plugins\android\lib\templates\activities
这里就是 Android Studio 存放代码模版的位置,找到 EmptyActivity 文件夹,这个名字是不是很熟悉?没错,我们正常 new 的 EmptyActivity 使用的就是这个代码模版,那么我们就用这个文件夹来完成我们的改造大业。
-
代码模版的解读。
-
globals.xml.ftl 文件
<?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"?> <#import "root://activities/common/kotlin_macros.ftl" as kt> <recipe> <#include "../common/recipe_manifest.xml.ftl" /> <@kt.addAllKotlinDependencies /> <#if generateLayout> <#include "../common/recipe_simple.xml.ftl" /> <open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" /> </#if> <#if generateKotlin> <instantiate from="root/src/app_package/SimpleActivity.kt.ftl" to="${escapeXmlAttribute(srcOut)}/${activityClass}.kt" /> <open file="${escapeXmlAttribute(srcOut)}/${activityClass}.kt" /> <#else> <instantiate from="root/src/app_package/SimpleActivity.java.ftl" to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /> <open file="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /> </#if> </recipe> 复制代码
这个文件主要是配置要引用的模板路径以及生成文件的路径,可以看到有是否生成 xml 布局文件的判断,选择生成 kotlin 还是 java 文件的判断。
<instantiate> 标签:from 引用的模版文件位置,to 生成的模版文件位置
<open file> 标签:打开生成的文件
当我们写好代码模版后,在这里配置对应的位置,Android studio 就会自动读取到我们写好的模版,生成代码。
-
template.xml 文件
<?xml version="1.0"?> <template format="5" revision="5" name="Empty Activity" minApi="9" minBuildApi="14" description="Creates a new empty activity"> <category value="Activity" /> <formfactor value="Mobile" /> <parameter id="instantAppActivityHost" name="Instant App URL Host" type="string" suggest="${companyDomain}" default="instantapp.example.com" visibility="isInstantApp!false" help="The domain to use in the Instant App route for this activity"/> <parameter id="instantAppActivityRouteType" name="Instant App URL Route Type" type="enum" default="pathPattern" visibility="isInstantApp!false" help="The type of route to use in the Instant App route for this activity" > <option id="path">Path</option> <option id="pathPrefix">Path Prefix</option> <option id="pathPattern">Path Pattern</option> </parameter> <parameter id="instantAppActivityRoute" name="Instant App URL Route" type="string" default="/.*" visibility="isInstantApp!false" help="The route to use in the Instant App route for this activity"/> <parameter id="activityClass" name="Activity Name" type="string" constraints="class|unique|nonempty" suggest="${layoutToActivity(layoutName)}" default="MainActivity" help="The name of the activity class to create" /> <parameter id="generateLayout" name="Generate Layout File" type="boolean" default="true" help="If true, a layout file will be generated" /> <parameter id="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" /> <parameter id="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" /> <parameter id="backwardsCompatibility" name="Backwards Compatibility (AppCompat)" type="boolean" default="true" help="If false, this activity base class will be Activity instead of AppCompatActivity" /> <parameter id="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> 复制代码
这里我们只看几处即可,
<template>
标签下:name="Empty Activity" 在向导界面中显示的模板名称
description="Creates a new empty activity" 界面中描述信息
<category value="Activity" /> 模板类型,可以自己定义
<formfactor value="Mobile" /> 所属类型,Mobile、Things、Wear等
<parameter>
标签:类似于我们 xml 布局中的 view,可以定义 id、name、type、derault等属性。
id="activityClass" 唯一标示,可以用${activityClass}获取value
name="Activity Name" 界面输入框前的提示文字
type="string" 类型 界面显示为输入框
type="boolean" 界面显示为勾选框
type="enum" 在界面中为下拉选择框
constraints="class|unique|nonempty" 约束 class必须是类名 unique唯一 nonempty不能为空
suggest="${layoutToActivity(layoutName)}" 建议的名称 默认生成的名称和布局文件名称关联
default="MainActivity" 默认值
help="The name of the activity class to create" 界面提示文字
-
SimpleActivity.java.ftl 文件
package ${packageName}; import ${superClassFqcn}; import android.os.Bundle; <#if includeCppSupport!false> import android.widget.TextView; </#if> public class ${activityClass} extends ${superClass} { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); <#if generateLayout> setContentView(R.layout.${layoutName}); </#if> <#include "../../../../common/jni_code_usage.java.ftl"> } <#include "../../../../common/jni_code_snippet.java.ftl"> } 复制代码
同样这个文件我们了解一下几点就好:
${packageName} 获取新建类时的包名
${activityClass} 获取新建类时的类名
${layoutName} 获取新建类时的布局文件名
创建我们自己的代码模版
终于来到我们的正题了,上面那些想要深入了解的可以仔细看看,如果只想做模版,就记住修改 recipe.xml.ftl 文件( 配置文件路径 )、template.xml 文件(模板配置信息,参数 )中的 <template> 标签中的 name 字段、<category> 标签中的 value 字段" />
这两处即可。
SimpleActivity.java.ftl 文件、SimpleContract.java.ftl 文件、SimplePresenter.java.ftl 文件、SimpleRepository.java.ftl 文件这几个就需要你自己根据你的实际项目需求自定义了。
提一下几个注意的点:
- 一般 Base 类、utils 类的包名会存在差异,建议模版中不导包,生成后手动导包。
- 一般 List 的泛型也是不确定的,模版中可以将泛型空出,根据实际 bean 类添加。
- 常用的一些参数、方法的框架也可以放入其中,将不同的地方空出,后补充。
- 新模版放入文件位置后,需要重启 Android Studio 才可以生效。
可以参考小编的自定义代码模版:github 链接地址
版权声明:本文为博主原创文章,可转载,转载请注明出处:https://juejin.im/editor/drafts/5acad7b95188252b0b20133b