android Studio 模版使用
来源:互联网 发布:网易上海房地产数据 编辑:程序博客网 时间:2024/06/06 09:10
由于最近在学习react native ,包括自己封装android原生的ui,但是在这个过程中需要生成对应了js文件和activity文件,每次都需要写一些重复的代码,不免是效率有所影响,所以正好了解下androidstudio的模板使用。其实关于模版的使用平时开发中已经使用到了,我们平时开发新建activity时就是用到了系统默认提供的一些模版,如下图所示
这些都是studio提供的一些Activty的一些模版,这些文件是在Android Studio安装目录下的。我的就是在
D:\AndroidStudio\plugins\android\lib\templates 这个目录下。大家可以在自己androidStudio目下找到。
那如何编写自己需要的模版了?接下来就看看系统原来模版文件。
打开plugins\android\lib\templates 下文件如下
我们之前看到的activity选择界面就是在activities 文件里 ,打开文件后里面有很多文件夹,每个文件夹就代表一个activity版本配置。
每个文件夹下结构如下:
接下来说下每个文件的作用
- template.xml
这个文件主要是设置模版显示ui的文件类似于activity 的layout文件,就是下图显示的内容
结构如下;<?xml version="1.0"?><template format="5" revision="5" name="ReactActivity" minApi="7" minBuildApi="14" description="Creates a ReactActivity"> <category value="Activity" /> <formfactor value="Mobile" /> <parameter id="generateLayout" name="Generate Layout File" type="boolean" default="true" help="If true, a layout file will be generated" /> ......... <!-- 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 format="5" revision="6" name="登陆界面" //模版标题 description="创建一个新的登陆界面" //该模版描述 requireAppTheme="true"//是否使用app主题 minApi="8" //Api最小版本 minBuildApi="14">parameter标签参数,也就是要在创建的时候自己设置的东西。每一个 parameter标签对应一个参数。这些参数会显示在创建页面上。也修改为中文。id :设置id,通过这个id获取对应的parrmeter的值name:界面上的label的提示语type : 输入值类型constraints:约束值suggest:建议值default:默认值help:底部显示的提示 <thumbs> 标签 该activity显示预览图,可以随意设置一个图片
2.globals.xml.ftl
从名字就看的出这个文件主要设置一些变量
包括生成代码父类,一些属性。
<global id="themeName" type="string" value="${themeName}" /> <global id="implicitParentTheme" type="boolean" value="${(themeNameNoActionBar?starts_with(themeName+'.'))?string}" /> <global id="themeNameNoActionBar" type="string" value="${themeNameNoActionBar}" /> <global id="themeExistsNoActionBar" type="boolean" value="${(theme.existsNoActionBar!false)?string}" /> <global id="themeNameAppBarOverlay" type="string" value="${theme.nameAppBarOverlay!'AppTheme.AppBarOverlay'}" /> <global id="themeExistsAppBarOverlay" type="boolean" value="${(theme.existsAppBarOverlay!false)?string}" /> <global id="themeNamePopupOverlay" type="string" value="${theme.namePopupOverlay!'AppTheme.PopupOverlay'}" /><#if !appCompat> <global id="superClass" type="string" value="Activity"/> <global id="superClassFqcn" type="string" value="android.app.Activity"/> <global id="Support" value="" /> <global id="actionBarClassFqcn" type = "string" value="android.app.ActionBar" />
3.recipe.xml.ftl
这个文件主要配置的就是生成文件参数的
copy :从root中copy文件到我们的目标目录,比如我们的模板Activity需要使用一些图标 ,那么可能就需要使用copy标签将这些图标拷贝到我们的项目对应文件夹。
merge : 合并模版文件到我们要生成的文件 from :模版文件位置 to 生成的文件位置
instantiate : 和merge 差不多就是新生成, from :模版文件位置 to 生成的文件位置
open:在代码生成后,打开指定的文件。
<copy from="root/res/drawable-hdpi" to="${escapeXmlAttribute(resOut)}/drawable-hdpi" /> <#if appCompat && !(hasDependency('com.android.support:appcompat-v7'))> <dependency mavenUrl="com.android.support:appcompat-v7:${buildApi}.+" /> </#if> <#if (buildApi gte 22) && appCompat && !(hasDependency('com.android.support:design'))> <dependency mavenUrl="com.android.support:design:${buildApi}.+" /> </#if> <merge from="root/AndroidManifest.xml.ftl" to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" /> <merge from="root/res/values/dimens.xml" to="${escapeXmlAttribute(resOut)}/values/dimens.xml" /> <merge from="root/res/values/strings.xml.ftl" to="${escapeXmlAttribute(resOut)}/values/strings.xml" /> <instantiate from="root/res/layout/activity_login.xml.ftl" to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" /> <instantiate from="root/src/app_package/LoginActivity.java.ftl" to="${escapeXmlAttribute(srcOut)}/${activityClass}.java" /> <open file="${escapeXmlAttribute(srcOut)}/${activityClass}.java" />
root 文件夹这个里面都是加的是ftl的代码文件和资源文件如下:
activity_login.xml.ftl
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="${relativePackage}.${activityClass}"> <!-- Login progress --> <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:visibility="gone"/> <ScrollView android:id="@+id/login_form" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/email_login_form" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"><#if (buildApi gte 22) && appCompat> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"></#if> <AutoCompleteTextView android:id="@+id/email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/prompt_email" android:inputType="textEmailAddress" android:maxLines="1" android:singleLine="true"/><#if (buildApi gte 22) && appCompat> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"></#if> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/prompt_password" android:imeActionId="@+id/login" android:imeActionLabel="@string/action_sign_in_short" android:imeOptions="actionUnspecified" android:inputType="textPassword" android:maxLines="1" android:singleLine="true"/><#if (buildApi gte 22) && appCompat> </android.support.design.widget.TextInputLayout></#if> <Button android:id="@+id/email_sign_in_button" style="?android:textAppearanceSmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="@string/action_sign_in" android:textStyle="bold"/> </LinearLayout> </ScrollView></LinearLayout>
这个就是对应要生成的xml文件配置的内容,可以设置自己需要的内容。中间可以夹杂<#if> 进行条件判断设置自己需要的效果。
这个文件夹可以根据文件类别建立对应的文件(可以是java文件xml,图片),并根据自己需要设置该中文件下自己想配置的内容。
最后把这些东西加进去后,重启androidStudio就可以了,在平时的开发中可以设置一些常用的模版如:
登录 ,意见反馈,欢迎界面等通用界面,提高开发效率。
最后补充下
关于ftl的语法可以参考这个传送
- android Studio 模版使用
- Chason Android Studio 注释模版的使用详解
- Android Studio注释模版设置
- Android Studio设置注解模版
- Android Studio 定制注视模版
- Android Studio修改注释模版
- Android Studio 中 .gitignore的模版
- android studio中轻松实现自定义模版
- Android studio模版提高开发效率
- Zend Studio 9 使用注释模版
- Android Studio自带模版:抽屉(DrawerLayout)布局
- Android Studio 中 proguard-rules.pro 的模版
- Android Studio常用代码模版,让编码飞起来 template
- 在 Android Studio中编程(插入动态模版)
- 模版方法模式在 android中使用
- 【android】 android studio使用
- android studio使用教程
- Android Studio使用教程
- 数组定义及使用
- SVN版本管理工具的使用
- 个人json学习
- 基础练习 Huffuman树
- JSONObject和JSONArray使用
- android Studio 模版使用
- 获取web.xml中的context-param和init-param定义的值
- Linux网络编程05——C/S与B/S架构的区别
- TCP全连接队列和半连接队列已满之后的连接建立过程抓包分析
- iOS判断设备WiFi是否打开
- P58 test3-6
- 总结一下自己的2016
- Linux网络编程06——UDP协议编程
- 关于Java泛型的若干问题