TIL #99

loci·2024년 8월 13일
0

TIL

목록 보기
92/111

Bottom navigation 생성

사용할 Fragment들부터 생성

// app/build.gradle.kts
 
// navigation
implementation (libs.androidx.navigation.fragment)
implementation (libs.androidx.navigation.ui)
    
//libs.versions
androidx-navigation-fragment = { module = "androidx.navigation:navigation-fragment", version.ref = "navigationFragment" }
androidx-navigation-ui = { module = "androidx.navigation:navigation-ui", version.ref = "navigationFragment" }

navigationFragment = "2.7.7"
// MainActivity.kt

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import com.example.dmz.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
        val navController = navHostFragment.navController
        NavigationUI.setupWithNavController(binding.navView, navController)

    }
}
// MainActivity.xml
<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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="30dp"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation_menu" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/nav_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation"/>

</androidx.constraintlayout.widget.ConstraintLayout>
// menu/bottom_nav_menu
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_browse"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/browse" />

    <item
        android:id="@+id/navigation_quiz"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/quiz" />
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/home" />
    <item
        android:id="@+id/navigation_search"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/search" />
    <item
        android:id="@+id/navigation_my_profile"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/my_profile" />
</menu>
// navigation/mobile_navigation

<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/mobile_navigation"
    app:startDestination="@+id/navigation_search">

    <fragment
        android:id="@+id/navigation_browse"
        android:name="com.example.dmz.ui.BrowseFragment"
        android:label="@string/browse"
        tools:layout="@layout/fragment_search" />
    <fragment
        android:id="@+id/navigation_search"
        android:name="com.example.dmz.ui.SearchFragment"
        android:label="@string/search"
        tools:layout="@layout/fragment_search" />
    <fragment
        android:id="@+id/navigation_home"
        android:name="com.example.dmz.ui.HomeFragment"
        android:label="@string/home"
        tools:layout="@layout/fragment_search" />
    <fragment
        android:id="@+id/navigation_quiz"
        android:name="com.example.dmz.ui.QuizFragment"
        android:label="@string/quiz"
        tools:layout="@layout/fragment_search" />

    <fragment
        android:id="@+id/navigation_my_profile"
        android:name="com.example.dmz.ui.MyProfileFragment"
        android:label="@string/my_profile"
        tools:layout="@layout/fragment_my_profile" />

</navigation>
profile
편리한 개발자

0개의 댓글