我想改变默认导航图标(后退按钮图标)到我的自定义图标。我不使用抽屉,只是一个简单的工具栏和材料组件。
这个是可能的吗?
发布于 2020-05-28 21:03:32
如果您正在使用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图标的方法,这是在非根目标上显示Up按钮的预期行为。
在安装方法和检查目标之后添加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);
}
}
});
发布于 2020-06-04 10:37:58
如果您已经创建了一个工具栏并将其设置为ActionBar
,如下所示
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
在设置自定义图标时有两个选项:
选项1
toolbar?.setNavigationIcon(R.drawable.homeNavigationIcon)
选项2
supportActionBar?.setHomeAsUpIndicator(R.drawable.homeNavigationIcon)
https://stackoverflow.com/questions/62018283
复制相似问题