ButterKnife的配置与使用

更新时间:2018-06-13

一、Module

1. 在Module中的配置

implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

2. 在Library中的配置

project:

classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0' //此处若为8.8.1会同步失败

library:

apply plugin: 'com.jakewharton.butterknife'
implementation  'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

二、使用

1. 在自定义view中使用

在构造方法中调用:

View view = LayoutInflater.from(context).inflate(R.layout.layout_title_bar, this);
ButterKnife.bind(this, this);   // 第1个参数为this或者view,第2个为this

2. 在library中使用注意:

(1) 查找组件:

R2替换R

@BindView(R2.id.iv)
ImageView mIv;

(2)onClick中

注解用R2,方法中用R,且不能使用switch-case方式

@OnClick({R2.id.btn1, R2.id.btn2})
public void onViewClicked(View view) {
    if (view.getId() == R.id.btn1) {

    } else if (view.getId() == R.id.btn2) {

    }
}

猜你喜欢

转载自blog.csdn.net/riqthen/article/details/80685322