赞
踩
When you create a new class file in Android Studio, it will automatically add a header to it. By default the header contains the user/author name and file created date. Something like ‘Created by user on date‘.
This header can be customized to change the name of the author and also to include other information.
Here’s how,
Bring up the Settings dialog, (File > Settings). Search for ‘File and Code Templates‘ or alternatively navigate toEditor > File and Code Templates. Click on the ‘Includes‘ tab and then select ‘File Header‘ from the left pane. The content of the ‘File Header‘ template will be displayed on the right pane.
Notice how the default template contains two variables ${USER} and ${DATE}. While creating a new file Android Studio assigns the values to these variables and adds the output text in the file header.
The description pane below contains details of other available predefined variables that you can use in the template, for example, ${PACKAGE_NAME} will insert name of the package or ${PROJECT_NAME} will insert the name of the project etc. You can include any of these variables in the template and Android Studio will use them while generating the file header. What’s more, you can define your own variables or assign a new value to a predefined variable like $USER. For example, let’s change the user (author) name to ‘John Smith’ ,
#set ($USER = "John Smith")
Let’s also define a few new variables,
- #set ($ORGANIZATION_NAME = "Super Awesome Company")
- #set ($CONTACT_EMAIL= "example@domain.com")
- #set ($PROJECT_URL= "http://urltomyawesomeproject.com")
Once the variables are defined and assigned values, you will need to include them in the template to actually use them. Here’s the completed template for reference,
Now, lets create a new class file to test our new file header template. Here’s our custom file header in all its glory.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。