美文网首页
Android自定义模板工程

Android自定义模板工程

作者: woochen123 | 来源:发表于2017-09-27 10:52 被阅读0次

目录

image.png

文件分析

1.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="activityClass"  //唯一标识
        name="Activity Name"    //名字 :左边标题
        type="string"   // 输入类型:字符串
        constraints="class|unique|nonempty" //约束:类/唯一/不能为空
        suggest="${layoutToActivity(layoutName)}"   // 建议值,比如填写ActivityName的时候,会给出一个布局文件的建议值
        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" />  //id对应的初始值
    <execute file="recipe.xml.ftl" />

</template>
2.globals.xml.ftl
<?xml version="1.0"?>
<recipe>

<copy from="root/res/drawable-hdpi"
            to="${escapeXmlAttribute(resOut)}/drawable-hdpi" />

<merge from="root/${resIn}/values/strings.xml.ftl"
             to="${escapeXmlAttribute(resOut)}/values/strings.xml" />

<instantiate from="root/src/app_package/SimpleActivity.java.ftl"
               to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" />

<open file="${escapeXmlAttribute(srcOut)}/${activityClass}.java" />

</recipe>
</recipe>
  • copy :从root中copy文件到我们的目标目录,比如我们的模板Activity需要使用一些图标,那么可能就需要使用copy标签将这些图标拷贝到我们的项目对应文件夹。
  • merge : 合并的意思,比如将我们使用到的strings.xml合并到我们的项目的stirngs.xml中
  • instantiate : 和copy类似,但是可以看到上例试将ftl->java文件的,也就是说中间会通过一个步骤,会生成root下面对应的代码,也就是说你待会root下面的Activity写了什么就会生成什么
  • open:在代码生成后,打开指定的文件,比如我们新建一个Activity后,默认就会将该Activity打开。
freeMark语法(EmptyActivity\root\src\app_package):
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">
}

小结

template.xml:他就是用来指定我们的参数,就是供用户选择和自定义
globals.xml.ftl : 它就是用来初始化一些参数,比如需不需要ActionBar 等等
recipe.xml.ftl : 它就是用来应用我们root文件夹下的资源,哪些需要copy哪些需要合并打开等等
root文件夹 存放对应源码的ftl文件,以及资源文件,待会生成的代码都会该文件夹下的一样

相关文章

网友评论

      本文标题:Android自定义模板工程

      本文链接:https://www.haomeiwen.com/subject/mnwsextx.html