Ale*_*289 7 android android-architecture-components android-jetpack
我将MainActivity作为导航控制器的主机,它具有工具栏和底部导航视图
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/navigation_graph"
app:defaultNavHost="true"
/>
<android.support.design.widget.BottomNavigationView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/colorPrimary"
app:itemIconTint="@color/color_bottom_view_navigation"
app:itemTextColor="@color/color_bottom_view_navigation"
app:menu="@menu/menu_bottom_view"
app:labelVisibilityMode="labeled"
android:id="@+id/bottom_nav"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
它会主办一些片段作为用于像导航底视图菜单HomeFragment,OrderFragment,FavouriteFragment,CartFragment,ProfileFragment。
假设在中有注销按钮HomeFragment,如果单击该按钮,它将移至“登录”屏幕。与往常一样,登录屏幕或注册屏幕没有底部导航视图,也没有工具栏。
那么,如果使用导航控制器,则删除该底部导航视图以及工具栏的最佳方法是什么?
我试图<Include> 在导航控制器图形中使用标签,
因此我制作了两个导航图,然后进行了2个活动以将片段放置为宿主。第一个活动具有底部导航视图和工具栏(MainActivity,就像我上面共享的xml一样),另一个活动没有底部导航视图和工具栏
导航图如下图所示:
但是当我从HomeFragment(具有注销按钮)转到LoginFragment使用以下代码时:
logout_button.setOnClickListener {
Navigation.findNavController(it).navigate(R.id.action_toAuthActivity)
}
Run Code Online (Sandbox Code Playgroud)
但在登录屏幕中,底部导航视图和工具栏仍然存在
我假设auth_graph(以AuthActivity作为主机)可用于承载一些没有底部导航视图和工具栏的屏幕,例如登录屏幕,注册屏幕或忘记密码屏幕。
但是....我无法使用这种方式删除该底部导航视图和工具栏
那么,如果使用导航控制器,如何删除某些片段中的底部导航视图和工具栏?
Rvb*_*b84 16
更简洁的是使用导航侦听器。这样,您只需要在 MainActivity 中使用 1 个函数,并且在要隐藏底部导航或任何其他 UI 元素(如工具栏)的所有片段中都不需要代码。将此函数放在 MainActivity 的 onCreate 中。
我的函数如下所示:
private fun visibilityNavElements(navController: NavController) {
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.about_fragment,
R.id.settings_fragment,
R.id.detail_fragment,
R.id.missionPhotoFragment -> bottom_nav?.visibility = View.GONE
else -> bottom_nav?.visibility = View.VISIBLE
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用Kotlin Android 扩展直接访问视图的 id(不需要 findViewById)。
| 归档时间: |
|
| 查看次数: |
1030 次 |
| 最近记录: |