ButterKnife
Butter Knife是专门用来为Android view进行注解的。使我们减少大量的findViewById的使用。
在studio项目的project 的build.gredle 文件中的dependencies标签下添加:
compile ‘com.jakewharton:butterknife:7.0.1’
在activity中绑定 ButterKnife.bind( this ) ;
在fragment中绑定 ButterKnife.bind( this ,view) ;
这里一定要注意,不能使用private和static,否则的话会报错: @BindView fields must not be private or static.
@Bind(R.id.btn)
Button btn;
如果大家不想自己写的话,那么可以在studio安装一个Zelezny插件
1.点击:studio-setting-Plugins
2.搜索:Zelezny
3.下载:插件
4.重启:studio
5.使用:光标移动到R.layout.xxx文件上,右键Generate… 这里注意一点光标移动到其他位置不会有
6.打开:Generate ButterKnife Injection 就可以一键生成了.