[英]Hamburger icon not showing correctly after toolbar replacement
我有一個具有專用於工具欄的片段布局的活動。 在中心,我們有一個普通的 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>
為了避免這個問題,我只是在前一個片段上添加了一個片段,然后刪除了添加的片段。
像這樣:
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()
}
這樣,在第一個工具欄中完成的所有自定義都不會丟失。
首先,您使用的是 Android 噴氣背包導航組件,還使用片段管理器手動執行片段事務。 您違背了使用噴氣背包導航組件的目的。 這就是使用導航庫的全部意義所在,這樣我們就不必手動進行片段事務了。 你在這里要做的就是把你的工具欄放在 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.