Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

android - BottomNavigationView中的选定项目与背景颜色相同(Selected Item in BottomNavigationView Has Same Color as Background)

I am implementing a bottom navigation menu.

(我正在实现底部导航菜单。)

but the first item in the menu is not selected.

(但是没有选择菜单中的第一项。)

I notice that when I change the color of bar background then it shows, and the reason is that the first item "enabled" color is the same as the background color of my navbar.

(我注意到,当我更改条形背景的颜色时,它就会显示出来,原因是第一项“启用”颜色与导航栏的背景色相同。)

How can I change the colors of the enabled tab as well as the remaining items.

(如何更改已启用的选项卡以及其余项目的颜色。)

By default they're black... let's say I want to change them to white.

(默认情况下,它们是黑色的。假设我要将其更改为白色。)

Thanks image of main activity.

(感谢主要活动的图像。)

http://pctechtips.org/apps/activity.png menu xml file

(http://pctechtips.org/apps/activity.png菜单xml文件)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />
</menu>

activity_main.xml

(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <org.techgeorge.textrecon.PaintView
        android:id="@+id/paintView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:menu="@menu/bottom_nav_menu" />

</RelativeLayout>
  ask by miatech translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to make drawable selector for your bottom navigation view, here is the example (nav_item_color_state.xml)

(您需要为底部导航视图创建可绘制的选择器,下面是示例(nav_item_color_state.xml))

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

then add codes below to your bottom navigation view

(然后在下方的导航视图中添加以下代码)

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

(app:itemIconTint =“ @ drawable / nav_item_color_state” app:itemTextColor =“ @ drawable / nav_item_color_state”)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...