Butter Knife 配置和使用及插件

简介: Butter Knife 配置和使用及插件

目前最新的版本是8.4.0的


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


GitHub:https://github.com/JakeWharton/butterknife


配置:


1.在app下的build.gradle中添加apply和compile


apply plugin: 'com.android.application'
<strong>apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.jakewharton.butterknife'</strong>
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    aaptOptions {
        cruncherEnabled = false
        useNewCruncher = false
    }
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    <strong>compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'</strong>
}

2.在project下的build.gradle中添加classpath

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.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
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

3.在Activity中配置并使用

class ExampleActivity extends Activity {
 @BindView(R.id.title) TextView title
  @BindView(R.id.subtitle) TextView subtitle;
  @BindView(R.id.footer) TextView footer;
  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(this);
    // TODO Use fields...
  }
}

4.绑定Button,参数可选


<span style="font-size:12px;">@OnClick(R.id.submit)
public void submit(View view) {
  // TODO submit data to server...
}</span>
@OnClick(R.id.submit)
public void submit() {
  // TODO submit data to server...

5.绑定ViewHolder

static class ViewHolder {
    @BindView(R.id.title) TextView name;
    @BindView(R.id.job_title) TextView jobTitle;
    public ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }

6.绑定资源

class ExampleActivity extends Activity {
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.graphic) Drawable graphic;
@BindColor(R.color.red) int red; // int or ColorStateList field
@BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field
  // ...
}
}


7.代码混淆

-keep class butterknife.** { *; }  
-dontwarn butterknife.internal.**  
-keep class **$$ViewBinder { *; }  
-keepclasseswithmembernames class * {  
    @butterknife.* <fields>;  
}  
-keepclasseswithmembernames class * {  
    @butterknife.* <methods>;  
}

8.插件Zelezny

可视化快速生产view


安装Preferences → Plugins → Browse repositories and search for ButterKnife Zelezny


或Preferences → Plugins → Install plugin from disk


GitHub地址:https://github.com/avast/android-butterknife-zelezny


使用(图是官网的):


鼠标放在布局上右键——>Generate——Generate ButterKnife Injections


image.png

目录
相关文章
|
Android开发
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
490 0
|
XML Java Android开发
Android旋转动画rotate动画,xml配置set实现
Android旋转动画rotate动画,xml配置set实现 作为快速备忘查询,写到这里记下。 在xml配置动画所需的set设置资源,然后上层Java代码以最少的代码实现一个匀速旋转的动画,这种开发场景在一些加载动画中比较常见,比如视频缓冲时候的加载动画。
2481 0
|
3天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8094 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10622 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全
|
11天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1595 5