您好,登錄后才能下訂單哦!
1》Android Studio 安裝ButterKnife插件
同安裝其他插件類(lèi)似,如下:
1.1》打開(kāi)Plugins界面
按照上圖中1,2,3指示操作(注意:這里我的Android Studio中已經(jīng)安裝了該插件,所以顯示的內(nèi)容不太一樣)。然后重啟Android Studio。
2》在項(xiàng)目上使用該開(kāi)源項(xiàng)目(以Android Studio 為例)
2.1》在bulid.gradle中添加依賴(lài)
重新編譯一下該項(xiàng)目,通過(guò)后繼續(xù)操作。
2.2》在代碼中就可以使用注解的方式了
2.2.1》示例布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/text_veiw_tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView 1" /> <Button android:id="@+id/button_bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button1" /> <TextView android:id="@+id/text_veiw_tv2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView 2" /> <Button android:id="@+id/button_bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button2" /> </LinearLayout>
2.2.2》在代碼中使用注解
選擇上述布局文件名,右鍵
選擇“Confirm”后,就會(huì)自動(dòng)生成各個(gè)在布局文件中帶有id 屬性的view的注解形式
如下所示:
@Bind(R.id.text_veiw_tv1) TextView textVeiwTv1; @Bind(R.id.text_veiw_tv2) TextView textVeiwTv2; @Bind(R.id.button_bt1) Button buttonBt1; @Bind(R.id.button_bt2) Button buttonBt2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); }
標(biāo)注如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。