碼字辛苦!轉(zhuǎn)載請注明出處!
0.前言
最近,AndroidStudio升級到了4.1版本,在使用ButterKnife時,使用BindView注解給出了這樣的提示:Resource IDs will be non-final in Android Gradle Plugin version 5.0,WTF?一個月改一次API的老谷歌又來坑開發(fā)者了???

image
是的,在未來,所有的R.id.*都會變成變量,盡管不知道這樣做是出于什么原因。

image
ButterKnife的作者也跳出來宣稱,開發(fā)已經(jīng)進入尾聲,將不再更新并棄用,推薦我們使用谷歌官方推出的 View Binding:
image
有趣的是,我在注意到這個改動前,推送了一個帶著這個警告的版本,而這個版本并沒有發(fā)生任何的異常崩潰。
也就是說,如果項目緊張,暫時保持不動也沒有問題,但我們?nèi)孕枰饾u的將使用ButterKnife的項目遷移到ViewBinding
1.使用View Binding
首先,我們需要激活這個工具,在app的build.gradle中,添加如下內(nèi)容:
android {
...
buildFeatures {
viewBinding = true
}
}
2.在Activity中使用
假設我們有一個activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
AndroidStudio會檢測所有的XML文件,使用駝峰法命名+Binding后綴,創(chuàng)建綁定類:比如activity_main,會生成一個ActivityMainBinding類。
我們這樣使用它:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//注意?。?!在setContentView之前!??!
ActivityMainBinding inflate = ActivityMainBinding.inflate(getLayoutInflater());
//注意?。。∵@里是inflate.getRoot(),不是R.layout.activity_main
setContentView(inflate.getRoot());
//這里的tv就是XML中的id為tv的TextView
inflate.tv.setText("Fxxk gooooooogie !!!");
}
用起來還是蠻簡單的,但這仍然掩蓋不了谷歌的迷惑行為,ButterKnife的注解式編程代碼更加清晰不是嘛~

image
3.在Fragment中使用
假設我們有一個fragement_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
我們這樣使用它:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
FragementMainBinding inflate = FragementMainBinding.inflate(inflater, container, false);
inflate.tv.setText("Holy G00gie");
return inflate.getRoot();
}