Android Studio自定义activity模板,成体系化的神级Android进阶笔记,

本文详细介绍了如何在Android Studio中创建自定义Activity模板,包括设置参数属性、引用全局变量和使用FreeMarker语法。通过自定义模板,可以提高Android开发效率,实现体系化的进阶学习。
摘要由CSDN通过智能技术生成
  • <template> 中的 name 属性,对应新建 Activity 时显示的名字

  • <category> 对应New的类别为Activity 上面parameter标签中部分属性如下:

  • id :唯一标识,最终通过该属性的值,获取用户输入值(文本框内容,是否选中)

  • name:界面上的类似label的提示语

  • type : 输入值类型

  • constraints:填写值的约束

  • suggest:建议值,比如填写ActivityName的时候,会给出一个布局文件的建议值。

  • default:默认值

  • help:底部显示的提升语 对应的android studio操作的图形化界面(上面代码有删减)

2.打开globals.xml.flt


<?xml version="1.0"?>

<#include “…/common/common_globals.xml.ftl” />

globals存储的是一些全局变量

3.recipe.xml.ftl


<?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)/{activityClas
s}.${ktOrJavaExt}" />

常用标签及其含义:

  • 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 >

<#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=“创建一个新的列表”>

<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” />

_${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” />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值