ButterKnife 用法

本文介绍了ButterKnife框架的功能特点及其在Android Studio中的配置方法,并提供了常用的使用场景示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.ButterKnife是什么?

  Square公司Jake Wharton大神开源了一个神奇的框架叫做ButterKnife,这个框架虽然也采用了注解进行注入,不过人家可是编译期生成代码的方式,对运行时没有任何副作用,见效快,只是编译期用一点点时间,其功能有

1.@BindView来替换调用findViewById()

2.使用@OnClick注解来消除由监听器带来的匿名内部类

3.通过对resource进行注解来消除多余的资源查找代码

4.多个View放在一个数组或者list中。然后一次性可以同时对这些View的动作,属性等进行操作

ButterKnife官网:http://jakewharton.github.io/butterknife/

二.BUtterKnife在Android studio中引用

在app下(局部)的build.gradle引用jar

compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'

如:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    //注解
    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
}

在开头apply plugin: 'com.android.application'后面加上

apply plugin: 'com.neenbedankt.android-apt'

如:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

在项目下(外部)的build.gradle的dependencies 里面加上

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

如:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

三.ButterKnife常见的使用情况

1.acticity中组件使用

在setContentView(layout_id)后面引入

ButterKnife.bind(this);

        在控件上使用@BindView(R.id.xx)进行绑定

 如:

@BindView(R.id.login_progress)
View mProgressView;
@BindView(R.id.login_form)
View mLoginFormView;

2.fragment中组件使用

3.事件使用

4.viewholder中使用

2.3.4使用方法看官网说明

四.Android ButterKnife Zelezny插件使用

 AS - File - plugins搜索框中输入ButterKnife,如果没有安装则搜索不到,没有安装点击 按钮,在Browse Repositories界面的搜索框中输入butterknife如下,点击Android ButterKnife Zekezny。点击右侧安装按钮安装,安装完成后重启即可。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值