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





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yechaoa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值