Selected tab's color in Bottom Navigation View

前端 未结 10 1941
故里飘歌
故里飘歌 2020-11-28 03:47

I\'m adding a BottomNavigationView to a project, and I would like to have a different text (and icon tint) color for the selected tab (to achieve greying out no

相关标签:
10条回答
  • 2020-11-28 03:53

    This will work:

    setItemBackgroundResource(android.R.color.holo_red_light)
    
    0 讨论(0)
  • 2020-11-28 03:56

    BottomNavigationView uses colorPrimary from the theme applied for the selected tab and it uses android:textColorSecondary for the inactive tab icon tint.

    So you can create a style with the prefered primary color and set it as a theme to your BottomNavigationView in an xml layout file.

    styles.xml:

     <style name="BottomNavigationTheme" parent="Theme.AppCompat.Light">
            <item name="colorPrimary">@color/active_tab_color</item>
            <item name="android:textColorSecondary">@color/inactive_tab_color</item>
     </style>
    

    your_layout.xml:

    <android.support.design.widget.BottomNavigationView
                android:id="@+id/navigation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?android:attr/windowBackground"
                android:theme="@style/BottomNavigationTheme"
                app:menu="@menu/navigation" />
    
    0 讨论(0)
  • 2020-11-28 04:00

    While making a selector, always keep the default state at the end, otherwise only default state would be used. You need to reorder the items in your selector as:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:color="@android:color/holo_blue_dark" />
        <item android:color="@android:color/darker_gray"  />
    </selector>
    

    And the state to be used with BottomNavigationBar is state_checked not state_selected.

    0 讨论(0)
  • 2020-11-28 04:03

    Instead of creating selector, Best way to create a style.

    <style name="AppTheme.BottomBar">
        <item name="colorPrimary">@color/colorAccent</item> 
    </style>
    

    and to change the text size, selected or non selected.

    <dimen name="design_bottom_navigation_text_size" tools:override="true">11sp</dimen>
    <dimen name="design_bottom_navigation_active_text_size" tools:override="true">12sp</dimen>
    

    Enjoy Android!

    0 讨论(0)
  • 2020-11-28 04:05

    It's too late to answer but might be helpful for someone. I was doing a very silly mistake, I was using a selector file named as bottom_color_nav.xml for Select and unselect color change but still it was not reflecting any color change in BottomNavigationView.

    Then I realize, I was returning false in onNavigationItemSelected method. It will work fine if you'll return true in this method.

    0 讨论(0)
  • 2020-11-28 04:06

    1. Inside res create folder with name color (like drawable)

    2. Right click on color folder. Select new-> color resource file-> create color.xml file (bnv_tab_item_foreground) (Figure 1: File Structure)

    3. Copy and paste bnv_tab_item_foreground

    <android.support.design.widget.BottomNavigationView
                android:id="@+id/navigation"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="0dp"
                android:layout_marginStart="0dp"
                app:itemBackground="@color/appcolor"//diffrent color
                app:itemIconTint="@color/bnv_tab_item_foreground" //inside folder 2 diff colors
                app:itemTextColor="@color/bnv_tab_item_foreground"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:menu="@menu/navigation" />
    

    bnv_tab_item_foreground:

     <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_checked="true" android:color="@color/white" />
            <item android:color="@android:color/darker_gray"  />
        </selector>
    

    Figure 1: File Structure:

    0 讨论(0)
提交回复
热议问题