问题
使用Android Studio 4.1 时,如果在布局中的UI控件之间添加注释,并且注释中包含“&”字符的话,viewBinding就无法识别注释之后的所有控件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvTest1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- & -->
<TextView
android:id="@+id/tvTest2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
解决办法
- 解决办法1:删除“&”字符。
- 解决办法2:升级Android Studio (用Android Studio 4.2 Beta 6版本进行测试,没有相关问题)。