Android Studio中ButterKnife插件的安装与使用
生活随笔
收集整理的這篇文章主要介紹了
Android Studio中ButterKnife插件的安装与使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
此文章來自http://www.jianshu.com/p/fe3c20f3ac27點擊打開鏈接
Android Studio中ButterKnife插件的安裝與使用
最近用到Android Butterknife Zelezny插件,感覺特別棒!在Android開發過程中,我們會寫大量的初始化控件和設置控件點擊事件的操作,這樣簡單而重復的工作讓人覺得麻煩而且沒意思,可以采用注解的方式去實現,而ButterKnife是注解中相對簡單易懂的開源框架。
一、Android Studio中安裝ButterKnife插件:
(1)和安裝其他插件一樣,首先,打開:settings->plugins界面
這里寫圖片描述
這里寫圖片描述
這里寫圖片描述
這里寫圖片描述
(2)重新啟動了Android Studio之后,此時我們已經安裝了ButterKnife插件了,接下來在項目的bulid.gradle中添加依賴,如下圖所示:
compile 'com.jakewharton:butterknife:7.0.0'這里寫圖片描述
(3)重新編譯一下該項目,就可以在代碼中使用注解的方式了,在這里我們先寫一個簡單的布局文件來舉例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/tv1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView1" /><Buttonandroid:id="@+id/btn1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Button1" /><Buttonandroid:id="@+id/btn2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Button2" /><ImageViewandroid:id="@+id/iv1"android:layout_width="match_parent"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher" /></LinearLayout> 如果不使用ButterKnife插件,我們需要在 重寫的onCreate()方法中通過findViewById()對控件進行實例化并對控件添加事件監聽,如下如所示:
這里寫圖片描述
代碼還是比較簡潔,這也是因為我們的布局相對而言比較簡單,可是如果布局比較復雜的話,這樣的操作就顯得比較繁瑣。現在我們只需要簡單點擊幾下鼠標就可以完成這些操作了。
(4)鼠標雙擊選擇布局文件名,右鍵(或者直接快捷鍵:Alt+Insert)
這里寫圖片描述
這里寫圖片描述 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的Android Studio中ButterKnife插件的安装与使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Android Studio中进行代码
- 下一篇: android 设置控件的透明度