ButterKnife8.4.0使用

项目github地址:https://github.com/JakeWharton/butterknife

ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,使代码更加简洁。

添加依赖:
在项目的project 的build.gredle 文件中的dependencies标签下添加。


buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'//添加此句

    }
}

在module的build.gredle 文件中添加

apply plugin: 'com.android.application'
apply plugin: 'android-apt'//添加此句

在module的build.gredle 文件中的dependencies标签中添加

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:support-v4:24.2.0'
    compile 'com.google.code.gson:gson:2.8.0'

    //butterknife,添加下面这两句
    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
}

安装插件
这里写图片描述

使用
有两个方法,一个在activity中的绑定,一个是在别的布局中绑定,这里我要绑定到一个自定义布局中,使用第二种方法,有两个参数的方法;
public class MainTitleBar extends LinearLayout {

public MainTitleBar(Context context) {
    super(context);
}

public MainTitleBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    mTitlebarView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.main_tilte_bar, this);
    //第一个参数为绑定目标,第二个参数为要绑定的view
    ButterKnife.bind(this, mTitlebarView);
}

}
鼠标箭头移动到R.layout.main_tilte_bar上,点击右键
点击Generate
这里写图片描述
点击Generate ButterKnife Injection
这里写图片描述
更具需要选择
这里写图片描述
生成效果
这里写图片描述

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值