我正在使用数据绑定(bind)将 View 绑定(bind)到 Activity ,它工作正常并且我能够通过数据绑定(bind)对象访问 View ,然后我更新了布局文件并添加了更多 View ,我再次构建项目但现在我仍然无法获得我添加的新 View ,它说无法解析符号。
我再次尝试构建该项目,但没有成功。
我尝试清理项目然后再次构建它没有用。
这是 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".DetailsActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/backdrop_height"
android:background="@color/textColorPrimaryLight">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:collapsedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="@dimen/dimen_title_text_margin_start"
app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax">
<ImageView
android:id="@+id/movieBackdrop_iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/image_placeholder_content_description"
android:scaleType="centerCrop"
app:srcCompat="@drawable/picture_placeholder" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/scrim" />
</FrameLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/app_bar_detail_activity"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentInsetStart="@dimen/dimen_inset_start"
app:layout_collapseMode="pin"
app:navigationIcon="@drawable/arrow_back_white" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<ImageView
android:elevation="10dp"
android:contentDescription="@string/image_placeholder_content_description"
android:id="@+id/moviePoster_iv"
android:layout_marginTop="160dp"
android:layout_marginStart="16dp"
android:layout_width="@dimen/small_poster_width"
android:layout_height="@dimen/small_poster_height" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</android.support.constraint.ConstraintLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
</layout>
最佳答案
数据绑定(bind)字段不会与您的 ID 相同。 ID 在 snake_case 中给出,最后您将通过 camelCase
获得您的 id 字段例如,如果您设置了 id movieBackdrop_iv
,那么您可以使用 mBinding.movieBackdropIv
。
使用建议,而不仅仅是复制粘贴。
更新
我也有像你一样的问题,只需使用清理、重建或运行项目来获取布局中新添加的 id 和变量。
关于android - 在布局文件中添加新 View 后,它们的 id 没有显示在 DataBinding 变量中,它说无法解析符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50696886/