Butter Knife 配置和使用及插件

举报
yechaoa 发表于 2022/05/30 23:38:54 2022/05/30
【摘要】 目前最新的版本是8.4.0的 官网:http://jakewharton.github.io/butterknife/ GitHub:https://github.com/JakeWharton/butterknife 配置: 1.在app下的build.gradle中添加apply和compile apply plugi...

目前最新的版本是8.4.0的

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

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

配置:

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


  
  1. apply plugin: 'com.android.application'
  2. <strong>apply plugin: 'com.neenbedankt.android-apt'
  3. apply plugin: 'com.jakewharton.butterknife'</strong>
  4. android {
  5. compileSdkVersion 24
  6. buildToolsVersion "24.0.3"
  7. aaptOptions {
  8. cruncherEnabled = false
  9. useNewCruncher = false
  10. }
  11. dependencies {
  12. compile fileTree(include: ['*.jar'], dir: 'libs')
  13. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  14. exclude group: 'com.android.support', module: 'support-annotations'
  15. })
  16. compile 'com.android.support:appcompat-v7:24.2.1'
  17. testCompile 'junit:junit:4.12'
  18. <strong>compile 'com.jakewharton:butterknife:8.4.0'
  19. apt 'com.jakewharton:butterknife-compiler:8.4.0'</strong>
  20. }
2.在project下的build.gradle中添加classpath


  
  1. buildscript {
  2. repositories {
  3. jcenter()
  4. }
  5. dependencies {
  6. classpath 'com.android.tools.build:gradle:2.2.0'
  7. classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
  8. classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  9. // NOTE: Do not place your application dependencies here; they belong
  10. // in the individual module build.gradle files
  11. }
  12. }
  13. allprojects {
  14. repositories {
  15. jcenter()
  16. }
  17. }
  18. task clean(type: Delete) {
  19. delete rootProject.buildDir
  20. }
3.在Activity中配置并使用


  
  1. class ExampleActivity extends Activity {
  2. @BindView(R.id.title) TextView title
  3. @BindView(R.id.subtitle) TextView subtitle;
  4. @BindView(R.id.footer) TextView footer;
  5. @Override public void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.simple_activity);
  8. ButterKnife.bind(this);
  9. // TODO Use fields...
  10. }
  11. }

4.绑定Button,参数可选


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

  
  1. @OnClick(R.id.submit)
  2. public void submit() {
  3. // TODO submit data to server...


5.绑定ViewHolder

  
  1. static class ViewHolder {
  2. @BindView(R.id.title) TextView name;
  3. @BindView(R.id.job_title) TextView jobTitle;
  4. public ViewHolder(View view) {
  5. ButterKnife.bind(this, view);
  6. }
  7. }
6.绑定资源


  
  1. class ExampleActivity extends Activity {
  2. @BindString(R.string.title) String title;
  3. @BindDrawable(R.drawable.graphic) Drawable graphic;
  4. @BindColor(R.color.red) int red; // int or ColorStateList field
  5. @BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field
  6. // ...
  7. }
7.代码混淆


  
  1. -keep class butterknife.** { *; }
  2. -dontwarn butterknife.internal.**
  3. -keep class **$$ViewBinder { *; }
  4. -keepclasseswithmembernames class * {
  5. @butterknife.* <fields>;
  6. }
  7. -keepclasseswithmembernames class * {
  8. @butterknife.* <methods>;
  9. }
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





文章来源: blog.csdn.net,作者:yechaoa,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yechaoa/article/details/52963196

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。