Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 안드로이드 스튜디오
- button
- ButtonWithButton
- 밑줄
- ROOM
- 뷰바인딩
- FRAGMENT
- textview
- RealtimeDB
- Activity
- 랜덤ID
- 안드로이드
- OutlinedButton
- androidstudio
- ViewModel
- Android
- EditText
- ArrayList
- firebasefunctions
- Imageview
- Kotlin
- 팝업액티비티
- MaterialButton
- Dialog
- BottomSheetDialog
- 코틀린
- lifecyclescope
- firebase
- 안드로이드스튜디오
- nav_graph
Archives
- Today
- Total
안드로이드 앱 개발
navigation을 활용 (+with bottomNavigationView) 본문
* 참고 블로그
1. 종속성 추가
//=============================navigation===============================
def nav_version = "2.3.5"
// Kotlin
implementation("androidx.navigation:navigation-fragment-ktx:$nav_version")
implementation("androidx.navigation:navigation-ui-ktx:$nav_version")
// Feature module Support
implementation("androidx.navigation:navigation-dynamic-features-fragment:$nav_version")
// Testing Navigation
androidTestImplementation("androidx.navigation:navigation-testing:$nav_version")
// Jetpack Compose Integration
implementation("androidx.navigation:navigation-compose:2.4.0-alpha02")
2. 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityMain"
android:background="@color/white"
android:visibility="visible">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<fragment
android:id="@+id/main_nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:menu="@menu/my_navigation_items"
app:labelVisibilityMode="labeled"
app:itemIconTint="@drawable/nav_menu_selector_color"
app:itemTextColor="@drawable/nav_menu_selector_color"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
3. res>navigation 폴더를 추가해주고
nav_graph.xml 파일을 형성한 후, 넣을 프래그먼트를 추가한다. (시작 프래그먼트 설정가능)
<?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.xml"
app:startDestination="@id/nav_home">
<fragment
android:id="@+id/nav_accountings"
android:name="com.example.today_seyebrowktver.FragmentAccounting"
android:label="FragmentAccounting" />
<fragment
android:id="@+id/nav_home"
android:name="com.example.today_seyebrowktver.Example5Fragment"
android:label="example_5_fragment"
tools:layout="@layout/example_5_fragment" />
<fragment
android:id="@+id/nav_customers"
android:name="com.example.today_seyebrowktver.FragmentCustomers"
android:label="FragmentCustomers" />
<fragment
android:id="@+id/nav_message"
android:name="com.example.today_seyebrowktver.FragmentMessage"
android:label="FragmentMessage" />
<fragment
android:id="@+id/nav_memo"
android:name="com.example.today_seyebrowktver.FragmentMemo"
android:label="FragmentMemo" />
</navigation>
4. bottomNavigationView의 xml
이떄 nav_graph의 id와 bottom의 item의 id가 일치해야한다.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_memo"
android:title="메모장"
android:icon="@drawable/memo2"/>
<item
android:id="@+id/nav_message"
android:title="문자"
android:icon="@drawable/message2"/>
<item
android:id="@+id/nav_home"
android:title="홈"
android:icon="@drawable/home0"/>
<item
android:id="@+id/nav_accountings"
android:title="매출"
android:icon="@drawable/graph2"/>
<item
android:id="@+id/nav_customers"
android:title="고객"
android:icon="@drawable/customer2"/>
</menu>
5. 마지막으로 액티비티에서 NavController로 세팅해주면 끝
private fun initNavigation(){
val navControler = findNavController(R.id.main_nav_host)
binding.bottomNavigation.setupWithNavController(navControler)
}