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

目录
相关文章
|
9天前
|
前端开发 Java 程序员
Spring Boot (十五): 优雅的使用 API 文档工具 Swagger2
Spring Boot (十五): 优雅的使用 API 文档工具 Swagger2
|
1月前
|
前端开发 Java
springboot集成knife4j接口文档
springboot集成knife4j接口文档
springboot集成knife4j接口文档
|
1月前
|
数据库
idea整合EasyCode基于lombok和swagger自定义模板
idea整合EasyCode基于lombok和swagger自定义模板
127 0
|
1月前
|
安全 Java Spring
如何在调试开发阶段,在已经配置了springsecurity的情况下还能使用swagger或者knife4j
如何在调试开发阶段,在已经配置了springsecurity的情况下还能使用swagger或者knife4j
42 0
|
1月前
|
前端开发 搜索推荐 Java
springboot集成swagger knife4j 最详细的步骤 手把手教你继承swagger
springboot集成swagger knife4j 最详细的步骤 手把手教你继承swagger
364 0
springboot集成swagger knife4j 最详细的步骤 手把手教你继承swagger
|
Java Maven 开发者
Springboot 整合 knife4j | Swagger文档最简单配置
Springboot 整合 knife4j | Swagger文档最简单配置
1096 0
Springboot 整合 knife4j | Swagger文档最简单配置
|
10月前
|
Java 程序员 API
SpringBoot项目使用Swagger2接口工具
使用RESTful服务通常是涉及到多个终端的团队,比如Android、iOS、WEB等。为了让大家沟通顺畅,通常我们需要编写一份详细的RESTful业务接口文档
82 0
|
10月前
|
Java API Maven
探索Knife4j Spring UI:优雅的API文档生成工具
在现代的应用开发中,API(Application Programming Interface)文档是团队合作和项目维护的关键。然而,编写和维护API文档可能是一个繁琐的任务。Knife4j Spring UI作为一款优雅的API文档生成工具,为我们提供了一种简单和高效的方式来生成易于阅读和测试的API文档。本文将深入探讨Knife4j Spring UI的基本概念、特点,以及如何在实际应用中使用它进行API文档管理和测试。
240 0
|
11月前
|
开发框架 Java API
SpringBoot集成knife4j
项目api编写有文档但是调试麻烦,需要借助postman/apipost等第三方工具,需要在开发框架中内置接口管理及调试功能增加开发便捷性
100 0
|
11月前
|
NoSQL Java Maven
自定义一个springboot的starter插件
springboot对第三方包的整合是由maven依赖和starter插件完成的。