본문 바로가기
Android Project/Extras..

BottomNavigation 텍스트, 아이콘 색상 변경

by juwon2 2024. 2. 14.

BottomNavigation의 메뉴를 선택시 아이콘의 색상과 텍스트 색상을 변경하는 방법을 알아볼것이다

 

 

먼저 drawable에 새로운 파일하나를 생성해서 클릭했을때의 색상과 클릭하지 않았을때의 색상을 지정해준다 android:state_checked 속성을 통해 클릭되었을 경우와 클릭되지 않은 경우를 구분한다

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 클릭했을 경우 -->
    <item android:state_checked="true" android:color="@color/main" />
    <!-- 클릭하지 않은 경우 -->
    <item android:state_checked="false" android:color="@color/bottomgray" />
</selector>

bottom_navigation_color.xml

 

 

activity_main.xml에 가서 만들어놓았던 bottomnavigation부분에 itemTextColor과 itemIconTint를 앞에서 만든 xml파일로 설정한다.

itemTextColor는 텍스트 색상의 변경이고, itemIconTint는 아이콘 색상 변경을 뜻한다

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <FrameLayout
        android:id="@+id/fl_"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bn_"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">
    </FrameLayout>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bn_"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu"
        app:itemIconTint="@drawable/bottom_navigation_color"
        app:itemTextColor="@drawable/bottom_navigation_color"/>


</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

 

 

 

이렇게하고 앱을 실행해주면 눌렀을때 아이콘과 텍스트색이 전부 잘 변경된것을 확인할수있다