Android导航组件。如何基于导航图对工具栏进行样式设计?

问题描述 投票:0回答:1

我有以下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);
android-architecture-components android-architecture-navigation
1个回答
0
投票

NavigationUI 使用一个 AppBarConfiguration 对象来管理导航按钮的行为。导航按钮的行为会根据用户是否在顶层目的地而改变。请看一下他们的文档 使用NavigationUI更新UI组件

© www.soinside.com 2019 - 2024. All rights reserved.