由于在两个片段之间的工具栏中实现后退按钮存在困难,我决定开始了解导航图。但是我遇到了一个错误。
“添加到FragmentContainerView的视图必须与Fragment相关联。 View android.widget.RelativeLayout{...}未与Fragment关联。”
以下是带有“nav_host_fragment”的.xml文件:
“添加到FragmentContainerView的视图必须与Fragment相关联。 View android.widget.RelativeLayout{...}未与Fragment关联。”
以下是带有“nav_host_fragment”的.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<layout ...
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack"
android:clickable="@{viewModel.isSearching ? false : true}"
android:focusable="@{viewModel.isSearching ? false : true}">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recent_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:background="@color/colorBlack"
tools:itemCount="10"
tools:listitem="@layout/search_user_layout" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:background="@color/colorBlack"
android:visibility="gone"
tools:itemCount="10"
tools:listitem="@layout/search_user_layout" />
<ProgressBar
android:id="@+id/loading"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true"
android:visibility="@{viewModel.isSearching ? View.VISIBLE : View.GONE}" />
</RelativeLayout>
</fragment>
</layout>
能否修复它?
<fragment>
标签内添加其他视图,而不是在外面添加? - ianhanniballake<fragment>
,就会出现错误只允许一个具有1个视图子元素的布局元素
。 - MaxB<fragment>
放在<RelativeLayout>
中,我会收到一个错误消息:View androidx.recyclerview.widget.RecyclerView{...} is not associated with a Fragment
。 - MaxB<fragment>
标签内的所有内容) - 这应该是一个单独的布局文件。 - ianhanniballake