ButterKnife使用详解
2017-02-16 本文已影响12人
来自唐朝的栗子
1.在app目录的gradle中添加
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
2.(1)在Activity的onCreate方法中的setContentView()方法后面添加一句代码:
ButterKnife.bind(this);
(2)在fragment的onCreateView()方法中:添加代码:
View view = inflater.inflate(R.layout.perfect, null);
ButterKnife.bind(this, view);
3.然后就可以使用了:
@BindView(R.id.textView)
TextView textView;
4.其实Android studio还有一个插件“ButterKnife Zelezny“,能够自动添加注解,下面奉上使用方法:
首先,安装ButterKnife Zelezny插件
重启生效。
然后,把光标移动到布局文件处(R.layout.activity_main),这一步至关重要!!!如图所示
右键单击
选中Generate
然后
点击Generate ButterKnife Injections,就可以自动生成注解了!
附带ButterKnife的官方网站 http://jakewharton.github.io/butterknife/