Android视图绑定:从包含的xml布局访问NavHostFragment时
我有activity_main.xml,启用了Android视图绑定:从包含的xml布局访问NavHostFragment时,android,layout,navigation,include,fragment,Android,Layout,Navigation,Include,Fragment,我有activity_main.xml,启用了ViewBinding可以生成ActivityMainBinding类。我有另一个content_main.xml布局,它包含在该布局中,如下所示: activity_main.xml <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
ViewBinding
可以生成ActivityMainBinding
类。我有另一个content_main.xml布局,它包含在该布局中,如下所示:
activity_main.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main"
android:id="@+id/main_container"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
更新:这在使用FragmentContainerView时有效。使用findNavController方法访问导航图。由于延迟了一些可见性,请尝试使用可运行的方法
Handler().postDelayed要承载导航图,应使用
FragmentContainerView
而不是Fragment
,并将name属性设置为:
android:name="androidx.navigation.fragment.NavHostFragment"
谢谢你的回答!我将尝试使用处理程序,但我的问题更具体到“视图绑定”。因为没有足够的声誉,我不能使用标签。请点击此链接:您想访问androidx.navigation.fragment.NavHostFragment的目的是什么?你可以通过它的id+id“nav_host”来访问它是的,我做了,它被修复了。我应该把答案写在上面。我更新了问题本身!
binding.apply {
//Works
mainContainer.textView.text = "test"
//throws "Cannot access 'android.widget.fragment'. Check your module classpath for missing or conflicting dependencies"
mainContainer.navHost.postDelayed({
binding.mainContainer.navHost.systemUiVisibility = FLAGS_FULLSCREEN
}, IMMERSIVE_FLAG_TIMEOUT)
}
android:name="androidx.navigation.fragment.NavHostFragment"