是否可以在导航组件中更改向上按钮图标? [英] Is it possible to change up button icon in Navigation Component?
问题描述
我想更改与导航组件一起使用的ActionBar中的向上按钮图标.我尝试了几种选择,例如:
I would like to change up button icon in ActionBar that is working with Navigation Component. I've tried several options like:
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_left_blue_24dp)
在MainAcitivty
或
app:navigationIcon="@drawable/ic_arrow_left_blue_24dp"
在Toolbar
.xml文件中,似乎没有任何作用.
in Toolbar
.xml file and nothing seems to work.
我使用
setSupportActionBar(appToolbar.toolbar)
setupActionBarWithNavController(this, navController)
在MainActivity:onCreate
方法中调用.
我希望
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_left_blue_24dp)
可以工作,因为例如通过调用以下内容来禁用ActionBar
的标题:
would work, because for example disabling title for ActionBar
by calling:
supportActionBar?.setDisplayShowTitleEnabled(false)
可以正常工作,并且导航时标题未设置为Fragment
名称.
works as expected and title is not set to Fragment
name while navigating.
此外,我进行了一些调查,在ActionBarOnDestinationChangedListener
中有一个对setNavigationIcon
的调用,该调用将图标设置为DrawerArrowDrawable
,这有点奇怪,因为我没有在其中使用Drawer
我的设置.
What's more, I investigated a little bit and in ActionBarOnDestinationChangedListener
there is a call to setNavigationIcon
which is setting an icon to DrawerArrowDrawable
, which is a little bit weird since I'm not using Drawer
in my setup.
也更改为:
setupWithNavController(toolbar, navController)
不起作用,因为ToolbarOnDestinationChangedListener
也使用相同的DrawerArrowDrawable
.
is not working because ToolbarOnDestinationChangedListener
also using the same DrawerArrowDrawable
.
推荐答案
我找到了答案.我检查了导航组件的问题跟踪器,现在看来,如果没有解决方法,就无法更改它:
I have found answer. I checked issue tracker for navigation component and it seems like for now it's impossible to change it without a workaround:
https://issuetracker.google.com/u/1/issues/121078028
很高兴仍然有可能,我们只需要实现OnDestinationChangedListener
并在那里更改图标,就象在AbstractAppBarOnDestinationChangedListener
中的setNavigationIcon
之后调用它一样.这是代码:
Gladly it's still possible, we just need to implement OnDestinationChangedListener
and change icon there as it's called after setNavigationIcon
in AbstractAppBarOnDestinationChangedListener
. Here is a code:
navController.addOnDestinationChangedListener { _, _, _ ->
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_left_blue_24dp)
}
您甚至可以为不同的目的地使用不同的图标.
You can even differ icon for different destinations.
这是临时解决方案,因为此功能尚不存在.我正在使用导航组件的1.0.0-alpha09
版本.
It's temporary solution as this feature is not there yet. I'm using 1.0.0-alpha09
version of the navigation component.
这篇关于是否可以在导航组件中更改向上按钮图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!