[안드로이드 Navigation 4] - Bottom Navigation

이영준·2023년 4월 13일
0

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/bottomFragment1">

    <fragment
        android:id="@+id/bottomFragment1"
        android:name="com.ssafy.fragment.e_bottom_navi.fragment.BottomFragment1"
        android:label="fragment_bottom1"
        tools:layout="@layout/fragment_bottom1" />
    <fragment
        android:id="@+id/bottomFragment2"
        android:name="com.ssafy.fragment.e_bottom_navi.fragment.BottomFragment2"
        android:label="fragment_bottom2"
        tools:layout="@layout/fragment_bottom2" />
    <fragment
        android:id="@+id/bottomFragment3"
        android:name="com.ssafy.fragment.e_bottom_navi.fragment.BottomFragment3"
        android:label="fragment_bottom3"
        tools:layout="@layout/fragment_bottom3" />
    <fragment
        android:id="@+id/bottomFragment4"
        android:name="com.ssafy.fragment.e_bottom_navi.fragment.BottomFragment4"
        android:label="fragment_bottom4"
        tools:layout="@layout/fragment_bottom4" />
</navigation>

id 값을 맞춰서 넣어준다.
id 값이 다르면 제대로 들어가지 않으니 주의해주자!!

bottom_nav_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/bottomFragment1"
        android:icon="@drawable/ic_my_page"
        android:title="@string/item1"
        />
    <item
        android:id="@+id/bottomFragment2"
        android:icon="@drawable/ic_empty_fridge"
        android:title="@string/item2"
        />

    <item
        android:id="@+id/bottomFragment3"
        android:icon="@drawable/ic_my_page"
        android:title="@string/item3"
        />
    <item
        android:id="@+id/bottomFragment4"
        android:icon="@drawable/ic_empty_fridge"
        android:title="@string/item4"
        />
</menu>

layout에 menu 삽입


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemIconTint="@drawable/menu_selector_color_icon"
        app:itemTextColor="@drawable/menu_selector_color_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_nav_menu"
        app:labelVisibilityMode="labeled"/>

activity에서 navigation 선언

        // 네비게이션 호스트
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host) as NavHostFragment

        // 네비게이션 컨트롤러
        val navController = navHostFragment.navController

        // 바인딩
        NavigationUI.setupWithNavController(binding.bottomNavigationView, navController)
profile
컴퓨터와 교육 그사이 어딘가

0개의 댓글