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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 无人驾驶安全报告分析

    摘要随着经济的快速发展,各国汽车保有量急剧增加,促使城市路况更加严峻繁杂,城市交通正面临着前所未有的巨大压力。加之疲劳驾驶、酒后驾驶等人为因素,使世界各国的交通事故率逐年上升,甚至多于世界大战死亡人数。随着汽车技术、信息通信技术与智能控制技术的高效融合,集自动控制、人工智能、体系结构视觉设计等众多技术于一体的无人驾驶汽车应运而生。通过在车辆内安装智能操纵控制系统与感应设备来获取信息用以控制车…

    2022年4月7日
    42
  • PyCharm 2021.9 激活码【2021免费激活】

    (PyCharm 2021.9 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月26日
    45
  • Android http Request / Response ContentType

    Android http Request / Response ContentType客户端在进行http请求服务器的时候,需要告诉服务器请求的类型,服务器在返回给客户端的数据的时候,也需要告诉客户端返回数据的类型。这个类型就是 ContentType ,不同的ContentType会影响客户端/服务器所看到的效果。contentType:告诉服务器,我要发什么类型的数据  1、默认的ContentType为 text/html也就是网页格式.   

    2022年7月19日
    12
  • js图片横排无限循环滚动

    js图片横排无限循环滚动一个好的插件地址:http://www.superslide2.com/demo.html<!DOCTYPEhtmlPUBLIC”-//W3C//DTDXHTML1.0Transitional//EN””http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”><htmlxmlns=”http://w…

    2022年7月18日
    30
  • Eclipse安装、使用及卸载

    Eclipse的安装、使用及卸载Eclipse安装网站下载安装包开始安装Eclipse使用开始使用Eclipse创建Java项目运行Java代码导入、导出项目Eclipse设置更新Eclipse卸载EclipseEclipse安装网站下载安装包Eclipse官网Eclipse下载点击左上方的Download进入Eclipse下载页面点击下方的Download进行下载(标黄的位置也可点击,可能安装方式不同)开始安装下载完成后,点击安装包开始安装点击EclipseIDEforj

    2022年4月6日
    68
  • pycharm2021.12.12 激活码【2021最新】

    (pycharm2021.12.12 激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月30日
    45

发表回复

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

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