底部导航视图中选定选项卡的颜色 [英] Selected tab's color in Bottom Navigation View
问题描述
我要在项目中添加BottomNavigationView
,并且希望为所选选项卡使用不同的文本(和图标色调)颜色(以使未选中的选项卡变灰).在颜色选择器资源文件中对android:state_selected="true"
使用其他颜色似乎无效.我还尝试使用android:state_focused="true"
或android:state_enabled="true"
添加其他项,但不幸的是没有效果.还尝试将state_selected
属性的默认(未选中)颜色设置为false(显式),没有运气.
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 non-selected tabs effect). Using a different color with android:state_selected="true"
in a color selector resource file doesn't seem to work. I also tried having additional item entries with android:state_focused="true"
or android:state_enabled="true"
, no effect unfortunately. Also tried setting the state_selected
attribute to false (explicitly) for the default (non-selected) color, with no luck.
这是将视图添加到布局中的方式:
Here is how I add the view to my layout:
<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/silver"
app:itemIconTint="@color/bnv_tab_item_foreground"
app:itemTextColor="@color/bnv_tab_item_foreground"
app:menu="@menu/bottom_nav_bar_menu" />
这是我的颜色选择器(bnv_tab_item_foreground.xml
):
Here is my color selector (bnv_tab_item_foreground.xml
):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/darker_gray" />
<item android:state_selected="true" android:color="@android:color/holo_blue_dark" />
</selector>
还有我的菜单资源(bottom_nav_bar_menu.xml
):
And my menu resource (bottom_nav_bar_menu.xml
):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_home"
android:icon="@drawable/ic_local_taxi_black_24dp"
android:title="@string/home" />
<item
android:id="@+id/action_rides"
android:icon="@drawable/ic_local_airport_black_24dp"
android:title="@string/rides"/>
<item
android:id="@+id/action_cafes"
android:icon="@drawable/ic_local_cafe_black_24dp"
android:title="@string/cafes"/>
<item
android:id="@+id/action_hotels"
android:icon="@drawable/ic_local_hotel_black_24dp"
android:title="@string/hotels"/>
</menu>
我将不胜感激.
推荐答案
在创建selector
时,始终将默认状态保留在末尾,否则将仅使用默认状态.您需要将选择器中的项目重新排序为:
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>
BottomNavigationBar
使用的状态是state_checked
而不是state_selected
.
And the state to be used with BottomNavigationBar
is state_checked
not state_selected
.
这篇关于底部导航视图中选定选项卡的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!