我有以下nav_graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/actualFragment">
<fragment
android:id="@+id/actualFragment"
android:name="com.online.view.ui.ActualFragment"
android:label="Актуальное"
tools:layout="@layout/fragment_actual" >
<action
android:id="@+id/action_actualFragment_to_actualEventFragment"
app:destination="@id/actualEventFragment"
app:enterAnim="@anim/nav_default_pop_enter_anim"
app:exitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@+id/actualEventFragment"
android:name="com.online.view.ui.detail.ActualEventFragment"
android:label="Актуальное"
tools:layout="@layout/actual_event_fragment" />
</navigation>
并且我的MainActivity具有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/coordinatorLayout"
android:layout_width="match_parent" android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:background="@color/white"
android:paddingLeft="8dp"
app:contentInsetStart="8dp"
app:contentInsetStartWithNavigation="0dp"
app:titleTextAppearance="@style/TabTitles"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:theme="@style/ToolbarStyle"
/>
<fragment
android:id="@+id/content"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="@+id/bottom_tabs"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.587"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_tabs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
app:labelVisibilityMode="labeled"
app:itemTextColor="@drawable/bottom_nav_colors"
app:itemTextAppearanceActive="@style/bottomBarTextSelected"
app:itemTextAppearanceInactive="@style/bottomBarText"
app:itemIconTint="@drawable/bottom_nav_colors"
android:alpha="1.0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/tabs_navigation"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
现在,当加载顶层片段及其子对象时,我希望活动工具栏具有不同的样式(在工具栏中显示向后箭头)。
问题::如果我使用Android导航组件,并且使用以下代码从根片段导航到子片段,如何为工具栏(appbar)实现这种不同的样式:
Bundle bundle = new Bundle();
bundle.putString("id", ei.id);
MainActivity)context).getNavController().navigate(R.id.action_actualFragment_to_actualEventFragment, bundle);
答案 0 :(得分:0)
NavigationUI
使用AppBarConfiguration
对象管理“导航”按钮的行为。导航按钮的行为会根据用户是否在顶级目的地而发生变化。看看他们的文档Update UI components with NavigationUI