使用 navhost 时无法更改工具栏后退图标
Can't change toolbar back icon while using navhost
我正在尝试(但没有成功)在使用 NavHostFragment
时更改工具栏上的后退箭头图标。我将 NavController
与工具栏一起使用(如您在此处所见)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
toolbar.setTitleTextColor(resources.getColor(R.color.toolbarTextColor))
navigation.setupWithNavController(Navigation.findNavController(this, R.id.nav_host_fragment))
}
导航工作正常,但我似乎无法更改后退图标。
我已经尝试过使用自定义主题。 None 个定义的属性到目前为止有效。
<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<item name="android:navigationIcon">@drawable/ic_keyboard_arrow_left_black_24dp</item>
<item name="android:homeAsUpIndicator">@drawable/ic_keyboard_arrow_left_black_24dp</item>
<item name="collapseIcon">@drawable/ic_keyboard_arrow_left_black_24dp</item>
<item name="android:closeIcon">@drawable/ic_keyboard_arrow_left_black_24dp</item>
</style>
尝试使用代码:
toolbar.navigationIcon = resources.getDrawable(R.drawable.ic_keyboard_arrow_left_black_24dp)
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp)
toolbar.navigationIconResource = R.drawable.ic_keyboard_arrow_left_black_24dp
使用代码时,布局创建后会出现后退按钮,但当我输入另一个片段时,图标会变回默认后退图标。
我该如何更改它?
将来应该添加此功能,目前您可以通过实现 'OnDestinationChangedListener' 并在回调中更改工具栏图标来实现(只需确保在设置后添加 'OnDestinationChangedListener'方法),像这样:
navController.addOnDestinationChangedListener { controller, destination, arguments ->
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp) //set it here for all the destinations, or inside the switch statement if you want to change it based on destination
when(destination.id) {
R.id.mainFragment -> {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp)
}
R.id.detailsFragment -> {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp)
}
}
}
基于这个问题:
我正在尝试(但没有成功)在使用 NavHostFragment
时更改工具栏上的后退箭头图标。我将 NavController
与工具栏一起使用(如您在此处所见)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
toolbar.setTitleTextColor(resources.getColor(R.color.toolbarTextColor))
navigation.setupWithNavController(Navigation.findNavController(this, R.id.nav_host_fragment))
}
导航工作正常,但我似乎无法更改后退图标。
我已经尝试过使用自定义主题。 None 个定义的属性到目前为止有效。
<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<item name="android:navigationIcon">@drawable/ic_keyboard_arrow_left_black_24dp</item>
<item name="android:homeAsUpIndicator">@drawable/ic_keyboard_arrow_left_black_24dp</item>
<item name="collapseIcon">@drawable/ic_keyboard_arrow_left_black_24dp</item>
<item name="android:closeIcon">@drawable/ic_keyboard_arrow_left_black_24dp</item>
</style>
尝试使用代码:
toolbar.navigationIcon = resources.getDrawable(R.drawable.ic_keyboard_arrow_left_black_24dp)
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp)
toolbar.navigationIconResource = R.drawable.ic_keyboard_arrow_left_black_24dp
使用代码时,布局创建后会出现后退按钮,但当我输入另一个片段时,图标会变回默认后退图标。
我该如何更改它?
将来应该添加此功能,目前您可以通过实现 'OnDestinationChangedListener' 并在回调中更改工具栏图标来实现(只需确保在设置后添加 'OnDestinationChangedListener'方法),像这样:
navController.addOnDestinationChangedListener { controller, destination, arguments ->
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp) //set it here for all the destinations, or inside the switch statement if you want to change it based on destination
when(destination.id) {
R.id.mainFragment -> {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp)
}
R.id.detailsFragment -> {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp)
}
}
}
基于这个问题: