我正在尝试将带有 findViewByIds 的现有项目转换为 ViewBinding。 ScrollView 之外的所有 View ID 都可以在 Android Studio 中与“binding.viewId ...”一起使用。 。”因此,我无法访问布局中 ViewFlipper 内的两个 我确认自动生成的 ViewBinding 类已经创建。下面是我试图将 ClickListener 附加到 downArrow 但我无法访问 downArrow 引用的地方。我在这里缺少什么? ViewBinding 不能在 ScrollView 中工作吗?我在这里缺少什么?//Activity
private ActivityEditBinding binding;
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityEditBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
// activity_edit.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".AddorUpdateCardActivity">
...
<RelativeLayout
android:id="@+id/secondLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar" >
<ScrollView
android:id="@+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/secondLinearLayout"
... >
<LinearLayout
android:id="@+id/lowerVFRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:orientation="horizontal" >
<ViewFlipper
android:id="@+id/expandVF"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginBottom="5dp" >
<include layout="@layout/expand_more_act_edit"
android:id="@+id/expandMore" />
<include layout="@layout/expand_less_act_edit"
android:id="@+id/expandLess" />
</ViewFlipper>
</LinearLayout>
</ScrollView>
</RelativeLayout>
// expand_more_act_edit.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/downArrow"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="sd"
android:clickable="true"
android:focusable="true"
android:src="@drawable/ic_expand_more" />
<!-- the down arrow -->
***Android Studio says "Cannot resolve symbol 'expandMore'***
binding.expandMore.downArrow.setOnClickListener(v -> {
// change the down arrow to the up arrow and show the 1-20 EditText lines.
expandVF.setDisplayedChild(expandVF.indexOfChild(findViewById(R.id.expandLess)));
moreViewStub.setVisibility(View.VISIBLE);
});
最佳答案
View binding 将从您在布局中提供的 id 生成 camelCase id。如果您的 View id 已经在布局中的 camelCase(expandVF) 中,有时 android studio 将无法识别,因为布局 View id 和绑定(bind)类 View ID 相同。这可能会产生“无法解析符号错误”。
所以不要使用 expandVF、expandMore、expandLess,而是使用 expand_vf、expand_more、expand_less。重建并运行,可能会起作用。
关于Android:ViewBinding 的 ScrollView 内没有可用的 View ID。如何访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67779226/