【问题标题】:Android - replace findViewById with ButterknifeAndroid - 用 Butterknife 替换 findViewById
【发布时间】:2017-08-25 10:24:03
【问题描述】:

我已成功将findViewByIds 替换为 Butterknife 库。 不幸的是,出现了一个问题:

final Button btnPopup = (Button) popupView.findViewById(R.id.btn_popup); //popupView.findViewById(...) problem!!!

如何在 Butterknife 中更改这行代码:Butterknife.bind(this); 占用整个活动 Views

【问题讨论】:

    标签: android findviewbyid butterknife


    【解决方案1】:

    使用

    将您的 popupView 绑定到您的主视图
    View popupView = View.inflate(getContext(), R.layout.yourPopup, null);
    ButterKnife.bind(this,popupView);
    

    或者你可能想直接使用绑定它

    Button btnPopup = ButterKnife.findById(popupView, R.id.btn_popup);
    

    【讨论】:

    • 有效。谢谢。顺利
    • 就拿第二个吧。这会膨胀您的弹出视图并找到对您的按钮的引用。欢迎您 ;)
    • 是的。我拿了第二名。
    • 谢谢,别担心。分数只是分数,帮助是有价值的;)但如果你想接受它,因为它帮助了你,请随意接受它。谢谢你。
    • 我接受它不是为了给你更多的分数,而是为了帮助其他用户知道有这样的解决方案
    【解决方案2】:

    首先,有两个依赖添加到使用ButterKnife库:

    构建.gradle

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

    然后,使用上面的@BindViewonCreate方法后在方法里面写ButterKnife(this, "any other unrelated views to this layout")

    “与此布局无关的任何其他视图”可以是另一个布局的内容,例如 弹出窗口 或 ....

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      相关资源
      最近更新 更多