【问题标题】:Change the color of bottom navigation bar item更改底部导航栏项的颜色
【发布时间】:2019-06-05 16:55:43
【问题描述】:

我将原色设置为某种橙色,我不明白为什么我的底部导航栏默认将白色而不是原色作为所选项目的颜色,如何解决?

底部导航栏的xml代码

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="0dp"
        android:background="@color/colorAccent"
        app:menu="@menu/bottom_navigation"/>

颜色(主要是某种橙色)

<color name="colorPrimary">#fb8c00</color>
<color name="colorPrimaryDark">#c25e00</color>
<color name="colorPrimaryLight">#ffbd45</color>
<color name="textOnPrimary">#000000</color>
<color name="colorAccent">#D81B60</color>

你可以看到选中的项目,文本是白色的,而原色是橙色

【问题讨论】:

    标签: android colors bottomnavigationview


    【解决方案1】:

    添加以下三个标签来控制Tabs的着色以及BottomNavigationView中Tabs中显示的Text

    <com.google.android.material.bottomnavigation.BottomNavigationView
        style="@style/Widget.MaterialComponents.BottomNavigationView.Colored"
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:background="@android:color/white"
        app:itemIconTint="@android:color/holo_orange_dark"
        app:itemTextColor="@android:color/holo_orange_dark"
        app:menu="@menu/menu_main" />
    

    它看起来像这样,

    不幸的是,我本可以创建 Drawable 来处理不同的状态,但是 itemIconTint 只接受颜色作为输入,而不是 Drawable。仍然非常接近您想要实现的目标。

    【讨论】:

    • 什么是选择器?这也不起作用,我想在未选择项目时使用黑色(如屏幕截图上的那个),在选择项目时使用原色(橙色)而不是白色
    • 我会检查一下并在一个小时内添加代码
    • 我已经编辑了我的答案,它适用于我。你只需要改变你想要的颜色值。我故意保留了这些价值观,以便您了解每个价值观的作用,如果它适合您,请不要忘记投票并接受
    • 我只希望所选项目是橙色而不是白色。我想要实现的是,ibb.co/hd3Bp6M 在另一个应用程序中更改颜色之间的原色并编写我在帖子中编写的代码就足够了,现在我不明白为什么所选项目是白色而不是橙色(在这种情况下,我将背景与主要重音放在一起,以便所选项目不会与背景的白色混淆)
    • 我已经编辑了我的答案,结果非常接近您想要实现的目标
    【解决方案2】:

    Step-1 在drawable文件夹下创建drawable文件并添加如下代码。

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#82C341" android:state_checked="true" />
    <item android:color="#a9a9a9" android:state_checked="false"/>
    </selector>
    

    Step-2 然后像这样使用这个drawable文件。 添加这两行。 应用程序:itemIconTint="@drawable/your_drawable_file_name" app:itemTextColor="@drawable/your_drawable_file_name"

    完整代码

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/tab_bg"
        android:layout_gravity="start"
        android:padding="0dp"
        android:focusable="false"
        app:itemIconTint="@drawable/your_drawable_file_name"
        app:itemTextColor="@drawable/bottom_navigation_color_selector"
        app:menu="@menu/menu_bottom_navigation"
        app:labelVisibilityMode="labeled" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多