BottomNavigationView 中的所选项目与背景颜色相同

mia*_*ech 3 android bottomnavigationview android-bottomnav material-components material-components-android

我正在实现底部导航菜单。但未选择菜单中的第一项。我注意到,当我更改栏背景的颜色时,它会显示出来,原因是第一个项目“启用”颜色与导航栏的背景颜色相同。如何更改已启用选项卡以及其余项目的颜色。默认情况下它们是黑色的......假设我想将它们更改为白色。感谢主要活动的图片。http://pctechtips.org/apps/activity.png 菜单 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />
</menu>
Run Code Online (Sandbox Code Playgroud)

活动主文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <org.techgeorge.textrecon.PaintView
        android:id="@+id/paintView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:menu="@menu/bottom_nav_menu" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 11

选定/未选定图标和标签的颜色由选择器的app:itemIconTint和属性定义。app:itemTextColor默认样式BottomNavigationView使用此选择器为图标和图标标签着色:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:alpha="1.0" android:color="?attr/colorPrimary" android:state_checked="true"/>
  <item android:alpha="0.6" android:color="?attr/colorOnSurface"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

检查是否由于某种原因颜色colorOnSurface和 与colorPrimary您的背景颜色相同BottomNavigationView

在任何情况下,您都可以覆盖这些颜色,而无需定义新的选择器
只需使用android:theme属性:

  <com.google.android.material.bottomnavigation.BottomNavigationView
      android:theme="@style/ThemeOverlay.BottomNavView"
      ../>
Run Code Online (Sandbox Code Playgroud)

和:

  <style name="ThemeOverlay.BottomNavView" parent="">
    <item name="colorPrimary">@color/secondaryDarkColor</item>
    <item name="colorOnSurface">@color/colorAccent</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

您还可以使用具有以下materialThemeOverlay属性的自定义样式:

  <style name="MyWidget.MaterialComponents.BottomNavigationView" parent="Widget.MaterialComponents.BottomNavigationView">
    <item name="materialThemeOverlay">@style/ThemeOverlay.BottomNavView</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

和:

  <com.google.android.material.bottomnavigation.BottomNavigationView
      style="@style/MyWidget.MaterialComponents.BottomNavigationView"
      ../>
Run Code Online (Sandbox Code Playgroud)

最后一个属性需要库的版本为1.1.0 。


Erw*_*n A 9

您需要为底部导航视图制作可绘制选择器,这是示例(nav_item_color_state.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_checked="true" />
    <item android:color="@color/darker_gray" android:state_checked="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)

然后将下面的代码添加到底部导航视图中

应用程序:itemIconTint =“@dr​​awable / nav_item_color_state”应用程序:itemTextColor =“@dr​​awable / nav_item_color_state”