前言
今天在微信上看到了其他人推送的文章,其中有一张效果图如下(借用鸿翔大神的图)
看了之后的我瞬间被它曼妙的身姿吸引了,遂决定研究一下~
概述
其实上图的效果就是我们android studio的模板,我们可以通过自定义模板方便我们日常的开发。
现在的android开发人员应该感觉到很幸运,android studio越来越稳定,构建速度越来越快,我们开发起来也越来越方便。作为一个android开发人员应该熟悉我们的开发工具。
开始
首先我们从android studio自带的模板说起,在android studio的安装目录下的\plugins\android\lib\templates\activities下保存了android studio系统自带的activity模板 截图如下:
通过模板可以快速的开发,但是由于我们UI大大的需求不会简单到使用系统自带的模板就可以完成的骚操作。
我们从最简单的SimpleActivity开始分析,先看目录结构
平常我们自定义模板通常是复制已有的模板出来,然后在上面修改在
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)}"
default="MainActivity"
help="The name of the activity class to create" />
<!-- 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
属性,对应新建Activity
时显示的名字<category>
对应New的类别为Activity
上面parameter标签中部分属性如下:(出自:【 Android Studio】自定义Activity模板)
- id :唯一标识,最终通过该属性的值,获取用户输入值(文本框内容,是否选中)
- name:界面上的类似label的提示语
- type : 输入值类型
- constraints:填写值的约束
- suggest:建议值,比如填写ActivityName的时候,会给出一个布局文件的建议值。
- default:默认值
- help:底部显示的提升语
对应的android studio操作的图形化界面(上面代码有删减)
2.打开globals.xml.flt
<?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>
通过里面标签我们很容易的猜测出globals存储的是一些全局变量
3.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>
<instantiate from="root/src/app_package/SimpleActivity.${ktOrJavaExt}.ftl"
to="${escapeXmlAttribute(srcOut)}/${activityClass}.${ktOrJavaExt}" />
<open file="${escapeXmlAttribute(srcOut)}/${activityClass}.${ktOrJavaExt}" />
</recipe>
常用标签及其含义:(出自【 Android Studio】自定义Activity模板)
- copy :从root中copy文件到我们的目标目录,比如我们的模板Activity需要使用一些图标,那么可能就需要使用copy标签将这些图标拷贝到我们的项目对应文件夹。
- merge : 合并的意思,比如将我们使用到的strings.xml合并到我们的项目的stirngs.xml中
- instantiate : 和copy类似,但是可以看到上例试将ftl->java文件的,也就是说中间会通过一个步骤,将ftl中的变量都换成对应的值,那么完整的流程是
ftl->freemarker process -> java
。 - open:在代码生成后,打开指定的文件,比如我们新建一个Activity后,默认就会将该Activity打开。
freemaker简单语法:
if 指令
//当价格为0时,就会打印出 "Pythons are free today!":
<#if animals.python.price != 0>
Pythons are not free today!
</#if>
if elseif else
<#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 指令
使用 include
指令, 我们可以在模板中插入其他文件的内容。
示例
一.template
<?xml version="1.0"?>
<template
format="5"
revision="5"
name="自定义列表的 Activity"
minApi="9"
minBuildApi="14"
description="创建一个新的列表">
<category value="Activity" />
<formfactor value="Mobile" />
<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" />
<!-- 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>
效果:
上面需要注意的就是使用输入的时候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)}" //首字母变成了小写
如:activityClass=“MainActivity” ——》 item_main
如:activityClass=“Main” ——》 item_main
其余两个文件参考simpleActivity
复杂的原理都是一样的,了解了这些就可以实现自己想要的模板了(在遇到问题的时候可以随时参考默认模板,总能找到你想要的^_^)
参考文章
- Android Studio自定义模板 写页面竟然可以如此轻松
- 神奇的Android Studio Template
- Android studio的自定义模板详解
- 自定义Android Studio工程模板
- 【 Android Studio】自定义Activity模板