简体   繁体   中英

Change color of navigation bar selected item

I used

setTheme(R.style.Theme_AppCompat);

to set my app theme darker. Now I run into that issue that the selected item in my navigationbar is relativly dark, so that users can hardly see it.

Is there anything that I can use in my style to get the selected navigation item in another color? Here is a picture of it

Here is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    tools:context="at.mrminemeet.asciimoji.MainActivity">

.....


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation"/>

</android.support.constraint.ConstraintLayout>

您可以将BottomNavigationView_itemBackground属性指定给BottomNavigationView,可以将选择器设置为背景,并为正常状态和选定状态提供不同的颜色

Add the following to your BottomNavigationView:

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

In your drawable folder, create a file bottom_navigation_selector.xml and add this to it:

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

Finally in every activity where you use this navigation bar add this line to onCreate() and onResume()

bottomNavigationView.setSelectedItemId(R.id.item);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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