Selected tab's color in Bottom Navigation View

前端 未结 10 1942
故里飘歌
故里飘歌 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 04:06

    If you want to change icons' and texts' colors programmatically:

    ColorStateList iconsColorStates = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_checked},
                        new int[]{android.R.attr.state_checked}
                },
                new int[]{
                        Color.parseColor("#123456"),
                        Color.parseColor("#654321")
                });
    
        ColorStateList textColorStates = new ColorStateList(
                new int[][]{
                        new int[]{-android.R.attr.state_checked},
                        new int[]{android.R.attr.state_checked}
                },
                new int[]{
                        Color.parseColor("#123456"),
                        Color.parseColor("#654321")
                });
    
        navigation.setItemIconTintList(iconsColorStates);
        navigation.setItemTextColor(textColorStates);
    
    0 讨论(0)
  • 2020-11-28 04:08

    In order to set textColor, BottomNavigationView has two style properties you can set directly from the xml:

    • itemTextAppearanceActive
    • itemTextAppearanceInactive

    In your layout.xml file:

    <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bnvMainNavigation"
                style="@style/NavigationView"/>
      
    

    In your styles.xml file:

        <style name="NavigationView" parent="Widget.MaterialComponents.BottomNavigationView">
          <item name="itemTextAppearanceActive">@style/ActiveText</item>
          <item name="itemTextAppearanceInactive">@style/InactiveText</item>
        </style>
        <style name="ActiveText">
          <item name="android:textColor">@color/colorPrimary</item>
        </style>
        <style name="InactiveText">
          <item name="android:textColor">@color/colorBaseBlack</item>
        </style>
    
    0 讨论(0)
  • 2020-11-28 04:11

    I am using a com.google.android.material.bottomnavigation.BottomNavigationView (not the same as OP's) and I tried a variety of the suggested solutions above, but the only thing that worked was setting app:itemBackground and app:itemIconTint to my selector color worked for me.

            <com.google.android.material.bottomnavigation.BottomNavigationView
                style="@style/BottomNavigationView"
                android:foreground="?attr/selectableItemBackground"
                android:theme="@style/BottomNavigationView"
                app:itemBackground="@color/tab_color"
                app:itemIconTint="@color/tab_color"
                app:itemTextColor="@color/bottom_navigation_text_color"
                app:labelVisibilityMode="labeled"
                app:menu="@menu/bottom_navigation" />
    

    My color/tab_color.xml uses android:state_checked

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

    and I am also using a selected state color for color/bottom_navigation_text_color.xml

    Not totally relevant here but for full transparency, my BottomNavigationView style is as follows:

        <style name="BottomNavigationView" parent="Widget.Design.BottomNavigationView">
            <item name="android:layout_width">match_parent</item>
            <item name="android:layout_height">@dimen/bottom_navigation_height</item>
            <item name="android:layout_gravity">bottom</item>
            <item name="android:textSize">@dimen/bottom_navigation_text_size</item>
        </style>
    
    0 讨论(0)
  • 2020-11-28 04:18

    Try using android:state_enabled rather than android:state_selected for the selector item attributes.

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