android - BottomNavigationView 中的选定项与背景颜色相同

标签 android bottomnavigationview material-components material-components-android android-bottomnav

我正在实现底部导航菜单。但未选择菜单中的第一项。我注意到,当我更改栏背景的颜色时,它会显示,原因是第一项“启用”颜色与我的导航栏的背景颜色相同。如何更改已启用选项卡以及其余项目的颜色。默认情况下它们是黑色的...假设我想将它们更改为白色。谢谢 主要 Activity 的形象。 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>

activity_main.xml

<?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>

最佳答案

选中/未选中图标和标签的颜色由 app:itemIconTintapp: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>

检查颜色 colorOnSurfacecolorPrimary 是否与 BottomNavigationView 的背景颜色相同。

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

  <com.google.android.material.bottomnavigation.BottomNavigationView
      android:theme="@style/ThemeOverlay.BottomNavView"
      ../>

与:

  <style name="ThemeOverlay.BottomNavView" parent="">
    <item name="colorPrimary">@color/secondaryDarkColor</item>
    <item name="colorOnSurface">@color/colorAccent</item>
  </style>

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

  <style name="MyWidget.MaterialComponents.BottomNavigationView" parent="Widget.MaterialComponents.BottomNavigationView">
    <item name="materialThemeOverlay">@style/ThemeOverlay.BottomNavView</item>
  </style>

与:

  <com.google.android.material.bottomnavigation.BottomNavigationView
      style="@style/MyWidget.MaterialComponents.BottomNavigationView"
      ../>

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

关于android - BottomNavigationView 中的选定项与背景颜色相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118872/

相关文章:

android - 导出 mupdf 和 android studio 0.5.4

java - 在循环中的 ArrayList.add() 之后,所有元素都是相同的。为什么?

java - 无法找到其他包中声明的 Activity

android-fragments - 在底部导航片段之间切换时工具栏会闪烁

dart - 如何创建自定义底部标签栏

android - 从 AutoCompleteTextView 中删除下拉箭头

Android Material 编辑文本 endIcon

android - 适用于 iOS 和 Android 的 AES GCM 加密库

Android BottomNavigationView 等距图标

android - 右侧带有图标的 Material 按钮