zxn-templates
bintray.user=zhang721688
bintray.apikey=838701a79a44c97075eda1019ee0a26428f4003e
介绍
android开发常用模板使用方法,以及配置过程.ASTemplates
settings.zip为常用配置,可以导入.
使用
该模版为配置好的模版,直接放置在指定位置即可使用
1,本机1放置路径:
C:\ZxnPrograms\Android\as-3.5.3\plugins\android\lib\templates\zxn-templates
2,本机2放置路径:
D:\ZxnProgram\Android\AS3.5.3\plugins\android\lib\templates
C:\ZxnPrograms\Android\as-3.5.3为as的安装目录.
3,如果更换电脑,只需将该文件夹下载到as安装目录\plugins\android\lib\templates\即可使用
4,每次模版改动后,需要重新启动as才能生效.
模版类型
Y:表示可用,N:表示不可用.
BlankFragment(系统模板)
ItemFragment(系统模板)
BaseActivity
BaseDialogFragment
BaseFragment
BaseQuickAdapter
ArrayAdapter
ArrayAdapterWithActivity
RecyclerViewAdapter
RecyclerViewActivity
FreeMarker2.3.20模板语言标签中的指令使用(预备知识)
as中使用的版本为FreeMarker2.3.20.
注释 #comment>
注释内容可见
注释内容不可见
if语句
语法结构:
Wow!#if>
语句案例1:
to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
#if>
导入R包
import ${applicationPackage}.R;
#if>
if...else语句
语法结构:
when-presentwhen-missing
#if>
语句案例1:
,大部分时候,freemarker会把 > 解释成标签结束!-->
及格!
不及格!
#if>
if...else if语句
recyclerview.layoutmanager = linearlayoutmanager(this)
recyclerview.layoutmanager = gridlayoutmanager(this,${listcolumn})
recyclerview.layoutmanager = staggeredgridlayoutmanager(${listcolumn}, staggeredgridlayoutmanager.vertical)
#if>
if...else if...else语句
语法结构:
...
...
...
语句案例1:
优秀!
良好!
一般!
#if>
字符串中的方法使用
获取指定索引字符
${user[4]}
user=zhangxiaoning
g
将指定字符拼接
${user[0]}${user[5]}${user[9]}
user=zhangxiaoning
zxn
截取子串1
${user[0..4]}
user=zhangxiaoning
zhang
截取子串2
${user[5..]}
user=zhangxiaoning
xiaoning
截取子串3
${user?keep_after_last("o")}
user=zhangxiaoning
ning
截取子串4
${user?keep_before("xiaoning")}
user=zhangxiaoning
zhang
替换部分子串
${"${adapterClass}"?replace("Adapter", "Holder")}
adapterClass=MyAdapter
MyHolder
删除部分子串
${"${adapterClass}"?replace("Adapter", "")}
adapterClass=MyAdapter
My
字符串转为小写
${"${adapterClass}"?lower_case}
adapterClass=My
my
拼接字符串
方法定义和使用
定义方法替换字符串
name=name?lower_case
name="item_rv_"+name
>
#function>
${defaultLayoutName("BaseAdapter")}
定义方法替换字符串
#function>
${generateHolder("BaseAdapter")}
定义方法计算平均数
#function>
${avg(10, 20)}
相对路径表示方式(预备知识)
./
代表目前所在的目录
../
当前目录的上一级目录
../../
当前目录的上一级目录的上一级目录
/xxx
当前目录的下一级目录
/
表示根目录
创建通用文件(配置一)
我们在common目录下分别创建放置AndroidManifest.xml.ftl,shared_manifest_macros.ftl,recipe_manifest_strings.xml.ftl,recipe_manifest.xml.ftl等文件.
AndroidManifest.xml.ftl
该文件中对生成组件在清单文件进行了配置.
android:name="${packageName}.${activityClass}"
android:launchMode="singleTop"
android:screenOrientation="portrait"/>
shared_manifest_macros.ftl
清单文件组成部分
android:host="${instantAppActivityHost}"
android:${instantAppActivityRouteType}="${instantAppActivityRoute}"
android:scheme="https" />
#if>
#if>
#macro>
recipe_manifest_strings.xml.ftl
清单文件引用的的文字部分配置
to="${escapeXmlAttribute(baseFeatureResOut)}/values/strings.xml" />
to="${escapeXmlAttribute(resOut)}/values/strings.xml" />
#if>
recipe_manifest.xml.ftl
配置清单文件来源和生成输出.
#if>
to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" />
模版配置BaseQuickAdapter
以BaseQuickAdapter为例子,进行配置过程分析.分别在BaseQuickAdapter目录下创建以下文件以及文件夹template.xml,recipe.xml.ftl,globals.xml.ftl,root.其中root目录下放置了布局模版和代码模版
root
SimpleAdapter.java.ftl,代码模版配置文件
package ${packageName};
import ${applicationPackage}.R;
#if>
/**
* Created by zxn on ${.now}.
*/
public class ${adapterClass}
extends BaseQuickAdapter {
public ${adapterClass}() {
super(${generateHolder("${adapterClass}")}.ITEM_LAYOUT_ID);
}
@Override
protected void convert(${generateHolder("${adapterClass}")} helper, ${beanClass} item) {
helper.onConvert(item);
}
static class ${generateHolder("${adapterClass}")} extends BaseViewHolder{
private static @LayoutRes int ITEM_LAYOUT_ID = R.layout.${layoutName};
${"${adapterClass}"?replace("Adapter", "Holder")}(View view) {
super(view);
ButterKnife.bind(this, view);
}
void onConvert(${beanClass} item) {
}
}
}
#function>
- item_linear.xml.ftl,布局模版配置文件1
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
- item_relative.xml.ftl,布局模版配置文2
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
template.xml
此文件配置了创建类文件时候的键入值,比如类名称,布局名称等等.
format="5"
revision="5"
name="BaseQuickAdapter"
minApi="7"
minBuildApi="14"
description="Creates a new Adapter that belongs to the BaseQuickAdapter subclass">
id="adapterClass"
name="Adapter Name"
type="string"
default="QuickAdapter"
help="The name of the adapter class to create" />
id="beanClass"
name="Bean Name"
type="string"
default="BaseInfo"
help="The name of the bean class for the first generic of adapter" />
id="layoutName"
name="Item Layout Name"
type="string"
default="item_rv_default"
help="The name of the item layout to create for the adapter" />
id="itemStyle"
name="Item Style"
type="enum"
default="linear"
help="select a root item style for adapter!" >
Root Layout (LinearLayout)
Root Layout (RelativeLayout)
recipe.xml.ftl
此文件配置了属性读取操作.
to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
#if>
to="${escapeXmlAttribute(srcOut)}/${adapterClass}.${ktOrJavaExt}" />
globals.xml.ftl
此文件中配置了需要使用的变量
recipe配置导入外部文件操作
配置当前模板中的common
配置默认模板中的common
globals.xml.ftl中的常用配置以及含义
java.ftl中的常用配置以及含义
import android.support.v7.app.ActionBarActivityandroid.app.Activity#if>;
implementation 'com.zxn.titleview:TitleView:1.0.7'
报错日志
----->
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.46'
maven { url "https://jitpack.io" }
implementation 'androidx.recyclerview:recyclerview:1.1.0'
Tip:
If the failing expression is known to be legally null/missing,
either specify a default value with myOptionalVar!myDefault,
or use
when-presentwhen-missing#if>.
(These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
The failing instruction (FTL stack trace):
----------
==> #if layoutStyle == "linear" [in template "root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" at line 5, column 5]
----------
"root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" in line 8, column 10:
Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> layoutStyle [in template "root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" at line 5, column 10]
Tip: If the failing expression is known to be legally null/missing,
either specify a default value with myOptionalVar!myDefault,
or use
when-presentwhen-missing#if>.
(These only cover the last step of the expression;
to cover the whole expression,
use parenthessis:
(myOptionVar.foo)!myDefault, (myOptionVar.foo)??
The failing instruction (FTL stack trace):
----------
==> #if layoutStyle == "linear" [in template "root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" at line 5, column 5]
----------
Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> generateLayout [in template "root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" at line 5, column 10]
Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use when-presentwhen-missing#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
The failing instruction (FTL stack trace):
----------
==> #if generateLayout && layoutStyle == ... [in template "root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" at line 5, column 5]
----------
//import com.chad.library.adapter.base.BaseQuickAdapter;
Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> ktOrJavaExt [in template "root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" at line 5, column 61]
Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use when-presentwhen-missing#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
The failing instruction (FTL stack trace):
----------
==> ${ktOrJavaExt} [in template "root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" at line 5, column 59]
Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> ktOrJavaExt [in template "root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" at line 5, column 61]
Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use when-presentwhen-missing#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
The failing instruction (FTL stack trace):
----------
==> ${ktOrJavaExt} [in template "root://zxn-templates/BaseQuickAdapter/recipe.xml.ftl" at line 5, column 59]
Error reading included file "root://../common/root/res/values/string.xml.ftl":
Template "root://../common/root/res/values/string.xml.ftl" not found.
Error reading included file "root://zxn-templates/BaseActivity/root/res/values/../common/root/res/values/string.xml.ftl":
Template "root://zxn-templates/BaseActivity/root/res/values/../common/root/res/values/string.xml.ftl" not found.
FreeMarker template error:
The following has evaluated to null or missing:
==> generateLayout [in template "\u003C#if (generateLayout)>true\u003C#else>false\u003C/#if>" at line 1, column 7]
Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use when-presentwhen-missing#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
addTitleRight