Androidx底部导航栏在可隐藏导航项目上保持高亮显示
当用户未通过身份验证时,我隐藏了一个底部导航项(该项将在用户通过身份验证后显示,在本例中,它是一个消息片段)。这很好,但当显示并选中该片段时,我所在的上一个导航菜单仍然高亮显示,即使我在该片段上。此外,一旦我选择了另一个导航项,消息仍然会在底部导航栏上突出显示。请参阅下面的屏幕截图:Androidx底部导航栏在可隐藏导航项目上保持高亮显示,android,android-fragments,androidx,android-architecture-navigation,android-bottomnav,Android,Android Fragments,Androidx,Android Architecture Navigation,Android Bottomnav,当用户未通过身份验证时,我隐藏了一个底部导航项(该项将在用户通过身份验证后显示,在本例中,它是一个消息片段)。这很好,但当显示并选中该片段时,我所在的上一个导航菜单仍然高亮显示,即使我在该片段上。此外,一旦我选择了另一个导航项,消息仍然会在底部导航栏上突出显示。请参阅下面的屏幕截图: 当尚未选择消息时 当我选择Message Fragment时(在此之前我是来自Home Fragment) 当我返回到消息之外的任何其他片段时(在本例中为Home片段) 这是我的主要活动(请注意,在底部导航栏
navView.menu.findItem(R.id.navigation\u messages).isVisible
,这是导致此行为的主要原因)
另外,这里是我的activity_main.xml文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:itemHorizontalTranslationEnabled="false"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
android:layout_marginBottom="56dp"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
,或者,在这种情况下不可行。删除android:background=“?android:attr/windowBackground”
可能使用itembackgorund和ItemIContentName问题仍然存在,它只是使图标颜色与所选颜色匹配。只有图标下面的文本会改变颜色(和大小)。在检查了其他代码后,我只是从菜单xml中取消了消息的分组。这会导致消息始终高亮显示的行为
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:itemHorizontalTranslationEnabled="false"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
android:layout_marginBottom="56dp"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>