工作日志:ButterKnife框架的理解与使用

来源:互联网 发布:vscode 离线插件包 编辑:程序博客网 时间:2024/06/04 19:43

ButterKnife框架的理解 
ButterKnife是一个应用与android系统的View注入框架,可以减少大量的findViewById以及setOnClickListenerr代码 
优势 
1 代码清晰,可读性强 
2 运行时 不会影响APP效率,使用配置方便 
3 方便处理Adapter里的ViewHolder绑定问题 
4 强大的View绑定和Click事件处理功能,简化代码,提升开发效率 
使用心得 
1 Activity ButterKnife.bind(this);必须在setContentView()之后,且父类bind绑定后,子类不需要在bind 
2 Fragment ButterKnife.bind(this,mRootView) 
3 属性布局中不能使用private或者static修饰,否则报错 
4 setContentView() 不能通过注解实现

配置 
1 项目的Gradle文件中:

    dependencies {    classpath 'com.android.tools.build:gradle:2.2.0'    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'}
  • 1
  • 2
  • 3
  • 4
  • 5

2 在Module中的Gradle文件中添加插件android-apt:

宁波整形医院http://www.lyxcl.org/
宁波整形美容医院http://www.lyxcl.org/

apply plugin: 'com.android.library'apply plugin: 'android-apt'
  • 1
  • 2
  • 3

3 在Module中的Gradle文件中添加依赖:

compile 'com.android.support:appcompat-v7:25.3.0'apt 'com.jakewharton:butterknife-compiler:8.0.1'compile 'com.jakewharton:butterknife:8.0.1'
  • 1
  • 2
  • 3
  • 4

android-apt: 
android_apt是一个Gradle插件,即工具,协助android studio处理注入式框架,它有两个目的: 
1 允许配置只在编译时作为注解处理器的依赖(编译时进行注解,并非运行时),而不添加到最后的APK或library中 
2 设置源路径(控件对应的R文件),使注解处理器生成的代码能被Android Studio正确使用

官网地址:http://jakewharton.github.io/butterknife/

原创粉丝点击