Android框架ButterKnife的使用详解,butterknife8.x.x版本的使用方法

butterknife是由Android大神JakeWharton所开发,项目地址

https://github.com/JakeWharton/butterknife/
 
 
  • 1

这里说一下8.1.0版本的使用,这个版本和以前的老版本使用方法修改了一下,不过也是比较简单的。

首先我们要在Module中build.gradle增加引入库:

/*增加注解的使用 butterknife*/
 compile 'com.jakewharton:butterknife:8.8.1'
 annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'


  • 1
  • 2
  • 3

还有Module中build.gradle添加构建:

//apply plugin: 'com.android.library'apply plugin: 'com.jakewharton.butterknife'



  
  
  • 1

然后我们需要在Project中build.gradle的depencises添加:

 classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'

repositories {
      mavenCentral()
    }

   
   
  • 1

然后最后我们就可以使用我们的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);
    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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();
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值