日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android studio创建文件,如何在Android Studio中创建File Templates

發布時間:2023/12/20 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android studio创建文件,如何在Android Studio中创建File Templates 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

標簽: File Template Android Studio

我發現一個可以讓寫程序變得簡單的方法,那就是自定義文件模板(Custom File Templates).那么什么是File Templates呢?說白了,就是一個已經包含一部分代碼的源文件

如何創建File Templates

首先,找到創建File Template的位置,依次打開File ==> Setting ==> Editor ==> File and Code Templates

File Templates Settings

之后點擊 +號添加一個File Template,這里需要填寫 File Template的名稱和代碼。在本次案例中,我們將創建一個實現了部分方法的RecyclerViewAdapter的File Template。因此就取名叫RecyclerViewAdapter

RecyclerViewAdapter

之后你就可以在下方的代碼區域粘貼或者編寫你的模板代碼了。這里有幾個預置的變量可以使用,當文件被創建的時候,這些預置的變量將會被 將會被對應的值替代,變量如下:

${NAME} 選擇的文件的文件名

${PACKAGE_NAME} 包名

${DATE} 系統當前的時間

當然,你也可以自定義變量,在本案例中,我們需要提供ViewHolder類和集合List的類型,所以自定義變量分別為${VIEWHOLDER_CLASS}和${ITEM_CLASS}

那么現在就來編寫用于創建模板的代碼,代碼如下:

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

import android.content.Context;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import java.util.List;

#parse("File Header.java")

public class ${NAME} extends RecyclerView.Adapter {

private final Context context;

private List items;

public ${NAME}(List items, Context context) {

this.items = items;

this.context = context;

}

@Override

public ${VIEWHOLDER_CLASS} onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.${LAYOUT_RES_ID}, parent, false);

return new ${VIEWHOLDER_CLASS}(v);

}

@Override

public void onBindViewHolder(${VIEWHOLDER_CLASS} holder, int position){

${ITEM_CLASS} item = items.get(position);

//TODO Fill in your logic for binding the view.

}

@Override

public int getItemCount() {

if (items == null){

return 0;

}

return items.size();

}

}

當你想要使用這個文件模板時,會提示你提供相關變量的值,比如${VIEWHOLDER_CLASS}和${ITEM_CLASS},填寫的值會自動替換對應的變量,非常的方便。

要使用已經定義好的File Template,只需要選中要創建位置的文件夾,然后點擊鼠標右鍵,再點“New”,你就可以在列表中看到剛創建的File Template的名字

Selecting custom template

點擊RecyclerViewAdapter后填寫相關變量的值

Fill In Custom Template Variables

填寫完后點擊"OK",之后你就可以看到自動生成的代碼了

Generated Class From Template

這樣,今后我要實現一個RecyclerView的Adapter時,就不用再去寫那么多代碼了

如果你有一些比較好的代碼模板,也可以分享給我哦 *_*,我的博客: http://www.bit100.com

@author zhongzilu

總結

以上是生活随笔為你收集整理的android studio创建文件,如何在Android Studio中创建File Templates的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。