Android Studio
[안드로이드 스튜디오] BottomNavigationView 사용하기
오늘은 집
2022. 5. 17. 19:02
1. 라이브러리 추가
implementation 'com.google.android.material:material:1.5.0'
sync 잊지말기!
2. res 에 menu 리소스 폴더를 추가하고 menu_bottom.xml 생성
3. menu_bottom.xml 에서 item (BottomNavigation 에 넣을 메뉴) 추가하기
여기서 만약 아이콘을 res 에 있는 xml 파일로 하고싶다면
android:icon="@drawable/menu_post_add_48"
과 같이 넣어주면 된다.
(3-2 여기서 아이콘 클릭시 색깔을 변경하고 싶다면 drawable에 item_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="#454114" />
<item android:color="#ff3344" />
</selector>
눌렀을 경우 갈색 아닐경우 빨간색으로 설정했을 때이다.
4. activity_main.xml 에 BottomNavigationView 추가
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemBackground="?colorPrimary"
app:itemIconTint="@drawable/item_color"
app:itemTextColor="@drawable/item_color"
app:menu="@menu/menu_bottom" />
</androidx.constraintlayout.widget.ConstraintLayout>
이후에는
Fragment를 추가하고 메뉴랑 연결지어주면 된다.
이는 다음 포스팅에서 다루겠다.