AndroidStudio 模板 Template 生成代码 - 代码模板好,下班回家早

android 中模板定义有3种
-
1.代码片段模板 (
Live Templates
) -
2.单文件模板 (
File and Code Templates
) -
3.多文件模板(
Templates
主要用于具有通用样式的activity、fragment、mvp、MVVM代码创建)
今天我们主要介绍的是第三种,系统为我们提供的通用代码模板相比大家已经熟悉了,今天咱们就创建一个属于我们自己的模板
简单普及下代码模板
如何快速导入一个Live Templates
默认代码模板放在在这里 C:\Users\Jhon\.AndroidStudio3.5\config\templates
你可以自己添加,当然也可以去下载其他人的
https://github.com/keyboardsurfer/idea-live-templates
https://download.youkuaiyun.com/download/bencheng06/12283700
下载完成直接复制到你的 Android Studio config\templates
目录下
概述
我们在使用Android Studio创建Activity、Fragment等等的时候,都会使用Android Studio提供的模板来简化我们创建的,使用模板时,我们只要做简单的配置,Android就能为我们生成相应的代码,所以使用模板可以提高开发的效率,接下来我们将学习如何去自定义一个符合自己项目框架的模板。
文章背景
我们知道,在Android开发中,官方提供了很多模板,可以直接创建成套的代码,很是方便开发

当你日夜加班写着重复的代码的时候有没有想过开发一个输入自己的代码模板,好吧就知道你想要,身体很诚实的小伙儿
一、首先你需要一个 TempateBuilder
插件安装

不要告诉我你不会哈,毕竟度娘这么发达
安装完成重启一下AS
二、建立自定义模板
以创建一个通用的列表act模板为例。
- 代码最终实现的样式是这样的

1.话不多说,依次创建好这三个模板文件
-
1.1 TempActivity.java
... public class ${TempClassName} extends BaseActivity implements OnRefreshLoadMoreListener { @Bind(R.id.rl1) RelativeLayout rl1; ... @Override public int getContentView() { return R.layout.${LayoutName}; } @Override public void initListener() { ... mAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.${LayoutItemName}, null) { @Override protected void convert(BaseViewHolder helper, String item) { } }; } ... }
-
1.2 activity 布局文件
LayoutName.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_schedule_list" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${packageName}.ui.${TempClassName}"> <include layout="@layout/layout_backtitle" /> <TextView android:layout_width="match_parent" android:layout_height="@dimen/qb_px_1" android:background="@color/color_hint_dcdcdc" /> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/mRefreshLayout" android:layout_width="match_parent" android:background="@color/color_bg_f0f0f0" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="@dimen/qb_px_3