【问题标题】:How to use Butterknife plugin in Android Studio? [duplicate]如何在 Android Studio 中使用 Butterknife 插件? [复制]
【发布时间】:2017-07-18 21:24:47
【问题描述】:

我目前正在一个使用 Butterknife 插件的项目中工作,我注意到类似 @BindView(R.id.something) 的东西。我们如何在应用中使用黄油刀插件?

【问题讨论】:

  • 阅读文档!! link
  • @Morl 或者说具体点,你会用它做什么
  • 您已经在使用 Butterknife 开发应用程序,但您甚至没有费心学习所使用的技术?每天都更令人震惊
  • 我要求您在发布问题之前先用谷歌搜索一下。沃顿本人涵盖了每种情况。
  • 如果你正确地阅读它的文档..你会知道如何使用它..

标签: android android-studio android-activity


【解决方案1】:

引自http://jakewharton.github.io/butterknife/

使用 @BindView 和视图 ID 注释字段,以便 Butter Knife 在布局中查找并自动投射相应的视图。

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...
  }
}

在 R.layout.simple_activity 中标识您的元素,然后使用 @BindView 将这些视图关联(也称为绑定)到您的活动中的变量。因此,ButterKnife 无需重复使用 findViewById(),而是为您完成所有这些工作。因此,标题、副标题、页脚将指向布局中的这些元素。

【讨论】:

    【解决方案2】:

    它将android视图绑定到方法和回调。

    View.findViewById(R.id.view_name) 的作用相同。

    要使用这个库,你必须初始化它,在onCreate:

    ButterKnife.bind(this);
    

    然后,例如声明:

    ImageView image = null;
    

    而不是

    image = (ImageView)parent.findViewById(R.id.view);
    

    您可以这样做:

    @BindView(R.id.view) ImageView image;
    

    【讨论】:

      【解决方案3】:

      查看文档:

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

      您必须在 gradle 中添加以下行:

      compile 'com.jakewharton:butterknife:8.5.1'
      annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-11
        • 1970-01-01
        • 1970-01-01
        • 2015-02-26
        • 1970-01-01
        • 2021-01-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多