Butter Knife(牛油刀)
程序员文章站
2022-06-17 09:39:26
...
-
添加依赖
android {
…
// Butterknife requires Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation ‘com.jakewharton:butterknife:10.2.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:10.2.1’
}Library projects
buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath ‘com.jakewharton:butterknife-gradle-plugin:10.2.1’
}
} -
安装 插件
打开file—》setting–》plugins–》Marketplace–搜索Butterknife -
使用
在BaseActivity的 onCreate 中初始化
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bind = ButterKnife.bind(this);
}
在onDestroy中解绑
@Override
protected void onDestroy() {
super.onDestroy();
bind.unbind();
}