我有一个布局,其中多次包含相同的子布局,每个子布局都有不同的角色:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<include
android:id="@+id/settings_eco_seekarc"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/settings_arc" />
<include
android:id="@+id/settings_comfort_seekarc"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/settings_arc" />
</LinearLayout>
如果我以这种方式找到视图,它会起作用:
View eco = root.findViewById(R.id.settings_eco_seekarc);
mEcoSeekArc = (SeekArc) eco.findViewById(R.id.settings_seekarc);
mEcoLeaf = (ImageView) eco.findViewById(R.id.settings_leaf_img);
mEcoText = (TextView) eco.findViewById(R.id.settings_text);
View cmf = root.findViewById(R.id.settings_comfort_seekarc);
mComfortSeekArc = (SeekArc) cmf.findViewById(R.id.settings_seekarc);
mComfortLeaf = (ImageView) cmf.findViewById(R.id.settings_leaf_img);
mComfortText = (TextView) cmf.findViewById(R.id.settings_text);
我现在在我的项目中引入 ButterKnife,我希望我可以简单地注释每个视图(以下显然不起作用,我可以看到原因)并稍后使用每个包含的布局根注入它们:
@InjectView(R.id.settings_seekarc)
SeekArc mEcoSeekArc;
@InjectView(R.id.settings_leaf_img)
ImageView mEcoLeaf;
@InjectView(R.id.settings_text)
TextView mEcoText;
@InjectView(R.id.settings_seekarc)
SeekArc mComfortSeekArc;
@InjectView(R.id.settings_leaf_img)
ImageView mComfortLeaf;
@InjectView(R.id.settings_text)
TextView mComfortText;
//then later...
View eco = root.findViewById(R.id.settings_eco_seekarc);
ButterKnife.inject(this, eco);
View cmf = root.findViewById(R.id.settings_comfort_seekarc);
ButterKnife.inject(this, cmf);
但是,以这种方式执行此操作会导致我在第二次注入时出现此错误:
错误:(81, 13) 错误:尝试对“mEcoSeekArc”上已注入的 ID 2131493185 使用 @InjectView。
我的问题是:有没有办法在这种情况下使用 ButterKnife?