工具栏替换后汉堡图标显示不正确

5

我有一个活动,其中包含专用于工具栏的片段布局。在中心区域,我们有一个常规的 nav_host 容器。

活动包含一个能够正确显示汉堡菜单图标的片段。如果我点击一个按钮,它将被第二个片段所取代,并替换旧工具栏为新的工具栏。但是,当我点击返回按钮后,新的工具栏会被旧的工具栏所替换。

问题在于汉堡菜单图标不会显示出来,工具栏也不能正常工作。有什么想法吗?

以下是活动中的代码:

open fun setupToolbar() {
    initialToolbar = ToolbarFragment()
    setToolbarFragment(initialToolbar){
        setNavController()
    }
}

fun setToolbarFragment(fragment: Fragment, callback: () -> (Unit) = {}) {
    supportFragmentManager
            .beginTransaction()
            .replace(R.id.toolbarContainer, fragment)
            .runOnCommit {
                callback.invoke()
            }.commit()
}

private fun setNavController() {
    val navController = findNavController(R.id.nav_host_fragment)
    val appBarConfiguration = setFragmentsWithHamburgerMenu(navController)
    toolbar.setupWithNavController(navController, appBarConfiguration)
}

fun setFragmentsWithHamburgerMenu(navController: NavController): AppBarConfiguration {
    return AppBarConfiguration(
            setOf(
                    R.id.analyticsFragment,
                    R.id.routinesFragment,
                    R.id.currentRunFragment,
                    R.id.myMapsFragment,
                    R.id.myRobotsFragment
            ),
            drawer_layout
    )
}

这是取代第二片段的代码:

override fun onResume() {
    super.onResume()
    val fragment = SearchToolbarFragment(searchHint, this::onQueryTextChange)
    baseActivity.setToolbarFragment(fragment) {
          // other stuff
    }

}

override fun onPause() {
    super.onPause()
    baseActivity.setupToolbar()
}

这是我的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable name="viewModel" type="[package].DashboardViewModel" />
    </data>

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/toolbarContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"/>

            <fragment
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbarContainer" />

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_nav"
                style="@style/BottomNavigationView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:itemBackground="@color/white"
                app:itemTextColor="@color/button_view_item"
                app:labelVisibilityMode="labeled"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:menu="@menu/menu_bottom_nav" />

            <LinearLayout
                android:id="@+id/progressMaskLayout"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbarContainer">

                <include layout="@layout/progress_mask" />
            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true">

            <include
                bind:viewModel="@{viewModel}"
                layout="@layout/element_navigation_bar"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </com.google.android.material.navigation.NavigationView>
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

1
你能具体说明一下你遇到的问题吗?工具栏显示正常吗?汉堡菜单图标是否被替换成了其他图标?等等... - tynn
你应该发布出现问题的屏幕截图。在我的情况下,当我将片段添加到一个活动中时,汉堡图标动画没有完全渲染。如果我不介意的话,我会增加一些时间来绘制完整的动画。 - CoolMind
这个回答解决了你的问题吗:https://dev59.com/seo6XIcBkEYKwwoYFQHr#30824390 - 10 Rep
2个回答

2
为了避免问题,我刚刚添加了一个片段到先前的一个上,并删除了添加的那个。
就像这样:
fun addToolbarFragment(fragment: Fragment, callback: () -> (Unit) = {}) {
    supportFragmentManager
            .beginTransaction()
            .add(R.id.toolbarContainer, fragment)
            .runOnCommit {
                callback.invoke()
            }.commit()
}

fun removeToolbarFragment(fragment: Fragment, callback: () -> (Unit) = {}) {
    supportFragmentManager
            .beginTransaction()
            .remove(fragment)
            .runOnCommit {
                callback.invoke()
            }.commit()
}

通过这种方式,第一个工具栏中进行的所有自定义操作都不会丢失。


0

首先,您正在使用Android Jetpack导航组件,但同时也在手动执行片段事务时使用片段管理器。这违背了使用Jetpack导航组件的目的。使用导航库的整个重点是,我们不必手动执行片段事务。在这里,您需要将工具栏放置在main_activity.xml中,以便所有片段都可以继承它。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<layout 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">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/Drawer_Main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/Layout_Coordinator_Main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/Toolbar_Main"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary">

                <TextView
                    android:id="@+id/Toolbar_Main_Title"
                    style="@style/Locky.Text.Toolbar.TitleText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/app_name" />

            </com.google.android.material.appbar.MaterialToolbar>

            <androidx.core.widget.NestedScrollView
                android:id="@+id/Nested_Scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="?attr/actionBarSize"
                android:fillViewport="true">

                <fragment
                    android:id="@+id/Navigation_Host"
                    android:name="androidx.navigation.fragment.NavHostFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:defaultNavHost="true"
                    app:navGraph="@navigation/navigation_drawer_main" />

            </androidx.core.widget.NestedScrollView>
            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/FAB_Add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_margin="@dimen/fab_margin"
                app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
                app:srcCompat="@drawable/ic_add" />

        </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/Navigation_View"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:clipToPadding="false"
            android:paddingStart="0dp"
            android:paddingEnd="16dp"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/menu_drawer_main" />

    </androidx.drawerlayout.widget.DrawerLayout>

</layout>

我已经添加了我的主活动布局。我将工具栏放在了main_activity.xml中。我这样做是错的吗? - Filnik
我更新了我的答案并添加了一些代码。像这样做,然后使用导航组件来更改你的片段。如果你需要修改工具栏的标题,只需在你的主活动中进行即可。 - Mervin Hemaraju

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接