<?xml version="1.0"?>
<#include “…/common/common_globals.xml.ftl” />
globals存储的是一些全局变量
<?xml version="1.0"?>
<#import “root://activities/common/kotlin_macros.ftl” as kt>
<#include “…/common/recipe_manifest.xml.ftl” />
<@kt.addAllKotlinDependencies />
<#if generateLayout>
<#include “…/common/recipe_simple.xml.ftl” />
</#if>
<instantiate from=“root/src/app_package/SimpleActivity.${ktOrJavaExt}.ftl”
to=" e s c a p e X m l A t t r i b u t e ( s r c O u t ) / {escapeXmlAttribute(srcOut)}/ escapeXmlAttribute(srcOut)/{activityClass}.${ktOrJavaExt}" />
</rec
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
ipe>
常用标签及其含义:
-
copy :从root中copy文件到我们的目标目录,比如我们的模板Activity需要使用一些图标,那么可能就需要使用copy标签将这些图标拷贝到我们的项目对应文件夹。
-
merge : 合并的意思,比如将我们使用到的strings.xml合并到我们的项目的stirngs.xml中
-
instantiate : 和copy类似,但是可以看到上例试将ftl->java文件的,也就是说中间会通过一个步骤,将ftl中的变量都换成对应的值,那么完整的流程是
ftl->freemarker process -> java
。 -
open:在代码生成后,打开指定的文件,比如我们新建一个Activity后,默认就会将该Activity打开。
freemaker简单语法:
//当价格为0时,就会打印出 “Pythons are free today!”:
<#if animals.python.price != 0>
Pythons are not free today!
</#if>
<#if >
<#elseif >
<#else >
</#if >
<#if animals.python.price < animals.elephant.price>
Pythons are cheaper than elephants today.
<#elseif animals.elephant.price < animals.python.price>
Elephants are cheaper than pythons today.
<#else>
Elephants and pythons cost the same today.
</#if>
使用 include
指令, 我们可以在模板中插入其他文件的内容。
============================================================
一.template
<?xml version="1.0"?><template
format=“5”
revision=“5”
name=“自定义列表的 Activity”
minApi=“9”
minBuildApi=“14”
description=“创建一个新的列表”>
<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=“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=“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=“generateAdapter”
name=“是否自动创建adapter”
type=“boolean”
default=“true”
help=“If true, a adapter file will be generated” />
<parameter
id=“adapterName”
name=“adapter Name”
type=“string”
constraints=“class|unique|nonempty”
suggest="${underscoreToCamelCase(classToResource(activityClass))}Adapter"
default=“MainAdapter”
visibility=“generateAdapter”
help=“The name of the adapter to create for the activity” />
<parameter
id=“generateItemLayout”
name=“是否自动创建item”
type=“boolean”
default=“true”
help=“If true, a item layout file will be generated” />
<parameter
id=“itemLayoutName”
name=“item 的 Layout Name”
type=“string”
constraints=“layout|unique|nonempty”
suggest=“item_${classToResource(activityClass)}”
default=“item_main”
visibility=“generateItemLayout”
help=“The name of the layout to create for the activity” />
<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” />
template_blank_activity.png
效果:
上面需要注意的就是使用输入的时候suggest这个属性,这里提供几种内置方法
这里注意几个内置函数:
1.suggest="${layoutToActivity(layoutName)}"
如:layoutName=“activity_main” ——> MainActivity
如:layoutName=“main” ——> MainActivity
2.suggest="${activityToLayout(activityClass)}"
如:activityClass=“MainActivity” ——> activity_main
如:activityClass=“Main” ——> activity_main
3.suggest="${underscoreToCamelCase(classToResource(activityClass))}Adapter"//首字母是大写
如:activityClass=“MainActivity” ——> MainAdapter
如:activityClass=“Main” ——> MainAdapter
4.suggest=“item_${classToResource(activityClass)}” //首字母变成了小写