android studio界面设计模板_android studio官网

android studio界面设计模板_android studio官网android studio模版功能简介

大家好,又见面了,我是你们的朋友全栈君。

序言

在使用android studio编写代码的时候,有一些模版功能能够增加开发效率,例如,当你输入logd 的时候,会出现如下图选项:

然后选择这一项按下enter键,就会自动出现如下代码:

Log.d(TAG, "onCreate: ");
复制代码

并且光标定位在冒号后面,这样就能方便的增加日志信息,下面来介绍一下该功能。

添加自己的模版

刚才的日志输出后的TAG没有定义,现在来创建一个TAG的模版。

进入android studio的设置界面后,然后打开如下地方:

然后点击面板右上方的家好店,就可以分别创建模版及模版组(模版指logd,模版组指AndroidLog),先选择user组,然后选择创建模版,在Abbreviation处填写输入唤起模版的关键词,对应于日志模版的 logd ,我们在这儿输入 tag ,Description处填写这一个模版的描述信息,在这儿填写 public static final String TAG = ""; ,然后在下面的内容区域填写之后模版要生成的内容,填写上 public static final String TAG = "$className$"; ,这时面板如下:

其中className变颜色了,这时模版的语法,使用两个$来标记这是带填充的内容,然后点击右侧的 Edit variables 按钮,为带填充的内容选择内容:

每一项都很好理解,我们在表达式处选择 className() ,然后点击下方的 Define 来设置这个模版能使用的地方。

我们选择java下面的 declaration,这样他能在java的class下面和字段同级的地方使用。

然后我们在class声明属性的地方输入tag:

按下enter就直接出现了如下代码:

public static final String TAG = "MainActivity";
复制代码

如此我们就成功添加了一个使用的自定义模版。

模版功能的更多介绍

在模版里输入双美元符号包裹的属性事,如果属性名输入相同的,最后能生成相同的值。如果不为属性选择函数,就需要自己去输入值,其的用法可以参见下一节的 快速生成retrofit的field注解

下面列举一些常用的属性的生成函数:

函数名 函数值
user() 当前计算机的用户名
date() 当前时间
methodName() 当前方法名
methodParameters() 当前方法的输入参数
methodReturnType() 当前方法的返回值类型
className() 当前类的类名
classNameComplete() 当前类的完整类名

还有一些其他的可以自行探索。

一些实用模版

下面介绍几个实用的模版。

页面的根布局

在xml文件里快速生成以LinearLayout为根布局的模版:

使用 DataBinding 的版本

供于测试的时候快速生成main方法的模版

Activity头部的注释

快速生成retrofit的field注解

可以将如下配置文件复制,保存为user.xml,放在android studio的配置目录里面直接引入这些模版,其中mac保存在 user/{用户名}/资源库/Preferences/AndroidStudio3.2/templates 下面,Windows保存在C盘下 .AndroidStudio3.2 的相应位置。

<templateSet group="user">
  <template name="noteActivity" value="/**&#10; * Activity描述信息:&#10; * &lt;p&gt;&#10; * 需要的intent参数:&#10; * &#10; * @author $USER$&#10; * @date $DATE$&#10; */&#10;" description="Activity头部注释" toReformat="false" toShortenFQNames="true">
    <variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
    <variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="api" value="/**&#10; * 描述:&#10; * @param $param$&#10; * @return $return$&#10; * @exception $exception$&#10; * @see IOException&#10; */" description="api注释" toReformat="false" toShortenFQNames="true">
    <variable name="param" expression="methodParameters()" defaultValue="" alwaysStopAt="true" />
    <variable name="return" expression="methodReturnType()" defaultValue="" alwaysStopAt="true" />
    <variable name="exception" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="fbc" value="($cast$) findViewById(R.id.$resId$);" description="findViewById with cast" toReformat="false" toShortenFQNames="true" deactivated="true">
    <variable name="cast" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="resId" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_EXPRESSION" value="true" />
    </context>
  </template>
  <template name="retrofitQuery" value="@Query(&quot;$name$&quot;) String $name$" description="retrofit Query参数" toReformat="false" toShortenFQNames="true">
    <variable name="name" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="retrofitField" value="@Query(&quot;$name$&quot;) String $name$" description="retrofit Field参数" toReformat="false" toShortenFQNames="true">
    <variable name="name" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="syso" value="System.out.println($message$);" description="System.out.println();" toReformat="false" toShortenFQNames="true">
    <variable name="message" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_STATEMENT" value="true" />
    </context>
  </template>
  <template name="tag" value="public static final String TAG = &quot;$className$&quot;;" description="public static final String TAG = &quot;&quot;;" toReformat="false" toShortenFQNames="true">
    <variable name="className" expression="className()" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="noteContentProvider" value="/**&#10; * ContentProvider描述信息:&#10; * &lt;p&gt;&#10; * &#10; * @author $USER$&#10; * @date $DATE$&#10; */&#10;" description="ContentProvider头部注释" toReformat="false" toShortenFQNames="true">
    <variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
    <variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="noteService" value="/**&#10; * Service描述信息:&#10; * &lt;p&gt;&#10; * &#10; * &#10; * @author $USER$&#10; * @date $DATE$&#10; */&#10;" description="Service头部注释" toReformat="false" toShortenFQNames="true">
    <variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
    <variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="noteBroadcastReceiver" value="/**&#10; * BroadcastReceiver描述信息:&#10; * &lt;p&gt;&#10; * 监听的广播:&#10; * &#10; * @author $USER$&#10; * @date $DATE$&#10; */&#10;" description="BroadcastReceiver头部注释" toReformat="false" toShortenFQNames="true">
    <variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
    <variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="noteCommon" value="/**&#10; * 类描述信息:&#10; * &#10; * @author $USER$&#10; * @date $DATE$&#10; */&#10;" description="java类头部注释" toReformat="false" toShortenFQNames="true">
    <variable name="USER" expression="user()" defaultValue="" alwaysStopAt="true" />
    <variable name="DATE" expression="date()" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="layoutRoot DataBinding" value="&lt;layout&gt;&#10;&#10; &lt;data&gt;&#10; &#10; &lt;/data&gt;&#10;&#10; &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&#10; xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&#10; xmlns:tools=&quot;http://schemas.android.com/tools&quot;&#10; android:layout_width=&quot;match_parent&quot;&#10; android:layout_height=&quot;match_parent&quot;&#10; android:orientation=&quot;vertical&quot;&gt;&#10;&#10; &lt;/LinearLayout&gt;&#10;&lt;/layout&gt;" description="页面的初始布局 DataBinding" toReformat="false" toShortenFQNames="true">
    <context>
      <option name="XML" value="true" />
    </context>
  </template>
  <template name="layoutRoot" value="&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&#10; xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&#10; xmlns:tools=&quot;http://schemas.android.com/tools&quot;&#10; android:layout_width=&quot;match_parent&quot;&#10; android:layout_height=&quot;match_parent&quot;&#10; android:orientation=&quot;vertical&quot;&gt;&#10;&#10;&lt;/LinearLayout&gt;&#10;" description="页面的初始布局" toReformat="false" toShortenFQNames="true">
    <context>
      <option name="XML" value="true" />
    </context>
  </template>
  <template name="main" value=" public static void main(String[] args) {&#10;&#10; }" description="main方法" toReformat="false" toShortenFQNames="true">
    <context>
      <option name="JAVA_DECLARATION" value="true" />
    </context>
  </template>
  <template name="mode" value="&lt;input type=&quot;button&quot; name=&quot;$method$&quot; value=&quot;$method$&quot; onClick=&quot;$method$();&quot; /&gt;&#10;&#10;function $method$() {&#10;&#10; $(&quot;result&quot;).innerText = &quot;$method$&quot;;&#10; var ret = HardwareInfo.$method$();&#10; $(&quot;result&quot;).innerText = &quot;$method$成功&quot; + ret;&#10;}&#10;" description="模板" toReformat="false" toShortenFQNames="true">
    <variable name="method" expression="" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_CODE" value="true" />
    </context>
  </template>
</templateSet>
复制代码

转载于:https://juejin.im/post/5c7caabd51882540ca1ccfbe

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/106956.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • hash碰撞解决方法

    hash碰撞解决方法Hash碰撞冲突我们知道,对象Hash的前提是实现equals()和hashCode()两个方法,那么HashCode()的作用就是保证对象返回唯一hash值,但当两个对象计算值一样时,这就发生了碰撞冲突。如下将介绍如何处理冲突,当然其前提是一致性hash。1.开放地址法开放地执法有一个公式:Hi=(H(key)+di)MODmi=1,2,…,k(k&lt;=m-1)其中,m为哈希表的表长。…

    2022年6月17日
    47
  • NSGA2 算法Matlab实现「建议收藏」

    NSGA2 算法Matlab实现「建议收藏」为了能随时了解Matlab主要操作及思想。故本文贴上NSGA-Ⅱ算法Matlab实现(测试函数为ZDT1)。更多内容访问omegaxyz.comNSGA-Ⅱ就是在第一代非支配排序遗传算法的基础上改进而来,其改进主要是针对如上所述的三个方面:①提出了快速非支配排序算法,一方面降低了计算的复杂度,另一方面它将父代种群跟子代种群进行合并,使得下一代的种群从双倍的空间中进行选取,从而保留了

    2022年5月19日
    42
  • 免费开源好用还佛系的国产PDF软件:pdf补丁丁下载 | 含pdf补丁丁使用手册[通俗易懂]

    免费开源好用还佛系的国产PDF软件:pdf补丁丁下载 | 含pdf补丁丁使用手册[通俗易懂]PDF补丁丁(PDFPatcher)是一款绿色免费开源且完全免费的多功能国产PDF软件,基于.NET开发,具有PDF编辑、PDF书签编辑、PDF书签导入/导出、PDF创建、PDF拆分、PDF批量重命名、PDF阅读、PDF合并、光学OCR文字识别、PDF图片和内容提取以及专为开发者同行准备的文档结构谈查起等一系列诸多实用功能,如果大家还不知道什么软件可以修改PDF文件的话,推荐大家务必试试免费开源还佛系并提供最新版本的pdf补丁丁下载。

    2025年8月22日
    3
  • Springboot项目使用动态切换数据源实现多租户SaaS方案

    Springboot项目使用动态切换数据源实现多租户SaaS方案

    2021年8月31日
    186
  • nginx源代码分析–读请求主体(1)

    nginx源代码分析–读请求主体(1)

    2022年1月10日
    53
  • 打印控件功能演示lodop_前端打印插件

    打印控件功能演示lodop_前端打印插件WEB打印控件Lodop技术手册目      录一、概述1二、系统文件1install_lodop32.exe和 install_lodop64.exe1LodopFuncs.js2三、控件参数6Caption6Color6Border6四、功能函数71、基本函数7VERSION7PRINT_INIT7SET_PRI…

    2025年9月16日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号