在 ButterKnife 中使用 @Bind 属性,如下所述。
@BindDrawable(R.drawable.ic_expand_small_holo_light) Drawable mExpandDrawable;
并在调用 setContentView 方法后的 onCreate 中,使用 ButterKnife 的 bind 方法(如果您使用的是 Activity)。
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// TODO Use fields...
}
如果您使用的是 Fragment,请使用以下代码初始化 ButterKnife:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
ButterKnife.bind(this, view);
// TODO Use fields...
return view;
}