10.3.干掉findById
在开始之前,假设存在某个布局文件,名称为 result_profile.xml
:
现在你要在java文件中查找该布局文件中的view。
你是否仍然使用findViewById
查找view?
findViewById
查找view?反复的findViewById有没有觉得很累,有没有更优雅的方式?
使用Butter Knife更优雅的查找view
Butter Knife是一个专注于Android系统的View注入框架,通过在字段上使用@BindView
代替indViewById
调用。
当然,Butter Knife的功能不仅仅如此哦!它还可以
Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
Eliminate anonymous inner-classes for listeners by annotating methods with
@OnClick
and others.Eliminate resource lookups by using resource annotations on fields.
view binding
View Binding是Android Studio 3.6推出的新特性,目的是为了替代findViewById
(内部实现还是使用findViewById)。
由于View Binding是Android Studio 3.6推出的新特性,升级Android Studio 至3.6及以上版本即可直接使用。
Last updated
Was this helpful?