简体   繁体   English

底部导航视图突出显示所选项目

[英]Bottom Navigation View highlight selected item

I have added Bottom Navigation View to my activity XMl. 我已将Bottom Navigation View添加到我的活动XMl中。 I am unable to set the default checked item from the menu items. 我无法从菜单项中设置默认选中的项目。 It always sets the first menu item as default. 它始终将第一个菜单项设置为默认值。 Also when I tap on other menu items the selected menu item is not hightlighted. 此外,当我点击其他菜单项时,所选菜单项不会突出显示。 If I programmatically set the item.setChecked(true) then the menu option gets highlighted but the first menu item is also highlighted. 如果我以编程方式设置item.setChecked(true),则菜单选项会突出显示,但第一个菜单项也会突出显示。 I am using 我在用

 compile 'com.android.support:design:25.0.1'

Here is the bottom Navigation view 这是底部的导航视图

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:itemBackground="@color/white"
    app:itemTextColor="@color/black"
    app:menu="@menu/navigation_menu">
</android.support.design.widget.BottomNavigationView>

Here is the menu 这是菜单

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_wallet"
    android:enabled="true"
    android:icon="@drawable/ic_account_balance_wallet_black_24dp"
    android:title="wallet"
    app:showAsAction="ifRoom"
    />
<item
    android:id="@+id/action_card"
    android:enabled="true"
    android:title="allowance"
    android:icon="@drawable/ic_credit_card_black_24dp"
    app:showAsAction="ifRoom"
    />

<item
    android:id="@+id/action_transaction"
    android:enabled="true"
    android:icon="@drawable/ic_description_black_24dp"
    android:title="transaction"
    app:showAsAction="ifRoom" />

</menu>

底部导航视图 Has anyone encountered this situation? 有谁遇到过这种情况? Any Suggestions on how to handle this? 关于如何处理这个问题的任何建议?

Thanks, Priya 谢谢,Priya

Add a selector.xml in drawable folder for example my xml is drawable文件夹中添加一个selector.xml ,例如我的xml

navbar_color.xml navbar_color.xml

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

Then add following in lines BottomNavigationView 然后在BottomNavigationView行中添加以下BottomNavigationView

app:itemIconTint="@drawable/selector"
app:itemTextColor="@drawable/selector"

I was not returning true from OnNavigationItemSelectedListener thats why I was facing the issue. 我没有从OnNavigationItemSelectedListener返回true,这就是我遇到问题的原因。 After returning true from OnNavigationItemSelectedListener the issue got resolved. 从OnNavigationItemSelectedListener返回true后,问题得到解决。 Thanks user2650128 for pointing in that direction. 感谢user2650128指向该方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM