butterknife是由Android大神JakeWharton所开发,项目地址
https://github.com/JakeWharton/butterknife/
这里说一下8.1.0版本的使用,这个版本和以前的老版本使用方法修改了一下,不过也是比较简单的。
首先我们要在Module中build.gradle增加引入库:
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
还有Module中build.gradle添加构建:
//apply plugin: 'com.android.library'apply plugin:
'com.jakewharton.butterknife'
然后我们需要在Project中build.gradle的depencises添加:
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
repositories {
mavenCentral()
}
然后最后我们就可以使用我们的butterknife了。简单写一下Activity的使用:
@BindView(R.id.tv)
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
fragment中看官方使用了解绑,我在这里这样使用:
/**
* ButterKnife的使用,官方在fragment中使用了解绑
*/
protected Unbinder unbinder;
@BindView(R.id.tv)
TextView tv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
unbinder = ButterKnife.bind(this, rootView);
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
unbinder.unbind();
}