导航组件-隐藏自定义工具栏中的向上导航图标和片段标签 [英] Navigation component - Hide navigation up icon and fragments label from the custom toolbar

本文介绍了导航组件-隐藏自定义工具栏中的向上导航图标和片段标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对我的应用程序使用自定义操作栏(带有自定义徽标和向上导航按钮)。

<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/white"/>

并在活动中设置此工具栏。

val navController = findNavController(R.id.nav_host_fragment_auth)
val appBarConfiguration = AppBarConfiguration(navController.graph)
val toolbar = findViewById<Toolbar>(R.id.toolbarAuth)
toolbar.setupWithNavController(navController, appBarConfiguration)

val mCustomView: View = layoutInflater.inflate(R.layout.custom_actionbar_auth, null)
val mButtonBack= mCustomView.findViewById<ImageView>(R.id.imageBack)
toolbar.addView(mCustomView)

自定义操作栏布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_actionbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:paddingStart="20dp"
android:paddingEnd="20dp">

<ImageView
    android:id="@+id/imageBack"
    android:layout_width="10dp"
    android:layout_height="15dp"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:background="@drawable/ic_arrow_back_white" />

<ImageView
    android:id="@+id/imageTitle"
    android:layout_width="115dp"
    android:layout_height="20dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/ic_actionbar_logo" />

但当我转到不同的片段时,它会在操作栏中显示默认的导航向上按钮和片段标签。如何删除/隐藏这些?

我也尝试了以下方法,但这些方法对我不起作用。

supportActionBar?.setDisplayHomeAsUpEnabled(false)
supportActionBar?.setHomeButtonEnabled(false)
supportActionBar?.setDisplayShowTitleEnabled(false)

toolbar.setNavigationIcon(null)

推荐答案

toolbar.setupWithNavController()方法调用setNavigationIcon()setTitle()Toolbar方法。这些方法对您的自定义图标或标题一无所知-它们只更新内置的导航图标和标题。

这意味着您永远不应该调用toolbar.setupWithNavController()

相反,如果您希望您的自定义工具栏布局对导航更改做出反应,您将希望遵循listening for navigation events的指导,并使用OnDestinationChangedListener来执行所需的任何自定义逻辑,而不是调用toolbar.setupWithNavController()

val navController = findNavController(R.id.nav_host_fragment_auth)
val toolbar = findViewById<Toolbar>(R.id.toolbarAuth)
val mCustomView: View = layoutInflater.inflate(R.layout.custom_actionbar_auth, null)
val mButtonBack = mCustomView.findViewById<ImageView>(R.id.imageBack)
toolbar.addView(mCustomView)
navController.addOnDestinationChangedListener { _, destination, arguments ->
    // Update the icon for your mButtonBack, etc.
}

如果您只想要向上导航图标的默认行为,可以直接调用navigateUp()

mButtonBack.setOnClickListener {
    navController.navigateUp()
}

这篇关于导航组件-隐藏自定义工具栏中的向上导航图标和片段标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆