【发布时间】:2020-09-13 00:40:51
【问题描述】:
【问题讨论】:
-
是的,这是可能的。你看过工具栏的文档吗?
-
是的,我做到了,我只能找到
navigationIcon属性,但这不会改变向上导航图标
标签: android android-toolbar androidx material-components-android material-components
【问题讨论】:
navigationIcon 属性,但这不会改变向上导航图标
标签: android android-toolbar androidx material-components-android material-components
如果您使用 Toolbar 来更改图标,只需使用:
Toolbar toolbar = findViewById(R.id.xxx);
toolbar.setNavigationIcon(R.drawable.xxxx4);
setSupportActionBar(toolbar);
如果您使用的是 ActionBar,您可以使用:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.xxx);
您还可以在您的应用主题中将其更改为覆盖 homeAsUpIndicator 属性:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="homeAsUpIndicator">@drawable/...</item>
</style>
如果您使用的是导航组件,目前无法自定义 HomeAsUpIndicator 图标,当您处于非 root 用户时,这是一种预期的行为,显示向上按钮目的地。
有一种解决方法,在您的设置方法后添加 addOnDestinationChangedListener 并检查目标。
比如:
navController.addOnDestinationChangedListener(
new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_xxx) {
//With ActionBar
//getSupportActionBar().setHomeAsUpIndicator(R.drawable.xxxx);
//With a Toolbar
toolbar.setNavigationIcon(R.drawable.xxxx);
}
}
});
【讨论】:
setupWithNavController 时,您的解决方案有效。用导航组件设置工具栏时有没有办法改变图标
Navigation Components 解决方法拯救了我的一天
如果您已创建工具栏并将其设置为ActionBar,如下所示
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
设置自定义图标有两种选择:
选项 1
toolbar?.setNavigationIcon(R.drawable.homeNavigationIcon)
选项 2
supportActionBar?.setHomeAsUpIndicator(R.drawable.homeNavigationIcon)
【讨论】: