ButterKnife作用
减少手写findviewbyid的操作,提高开发效率,可使用Android Studio一键生成代码
引入步骤
- 添加依赖
在build.gradle(app)中:
dependencies {
//butterknife
implementation 'com.jakewharton:butterknife:10.2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
}
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
在build.gradle(Project)中
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.0'
}
}
- 集成插件
在Android Studio中File>Setting>plugin搜索butterknife下载
- 使用
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
ButterKnife.bind(this);
}
将鼠标放在XML资源ID上右键>Generate>Generate ButterKnife Injections,勾选要生成的选项,完毕
ButterKnife是一款用于Android开发的库,能够显著减少findViewById的使用,提升开发效率。通过一键生成代码,简化Activity和Fragment中的视图绑定过程。本文详细介绍了ButterKnife的引入步骤、依赖配置及使用方法。

被折叠的 条评论
为什么被折叠?



