와챠의 우당탕탕 개발 기록장
[안드로이드] 하단 메뉴(BottomNavigationView) 본문
반응형

하단에 요로코롬 네비게이션으로 프레그먼트를 이동하게 만들어보자.
색깔은 구분 쉬우라고 일부러... 저렇게 했음
activity_main.xml

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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" | |
android:orientation="vertical"> | |
<FrameLayout | |
android:id="@+id/container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_weight="9"> | |
</FrameLayout> | |
<com.google.android.material.bottomnavigation.BottomNavigationView | |
android:id="@+id/bottomNavi" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:menu="@menu/menu_tabs" | |
app:itemBackground="?attr/colorPrimary" | |
app:itemIconTint="#ff0000" | |
app:itemTextColor="#00ff00" | |
android:layout_weight="1"/> | |
</LinearLayout> |
menu_taps.xml

MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.mysnskakao | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// 시작은 프레그먼트 A로 초기화 | |
with(supportFragmentManager.beginTransaction()) { | |
var fragmentA = FragmentA() | |
replace(R.id.container, fragmentA) | |
commit() | |
} | |
// 네비게이션 | |
bottomNavi.setOnNavigationItemSelectedListener { | |
when(it.itemId) { | |
R.id.tab1 -> { | |
// container 부분에 FrameA 넣기 | |
with(supportFragmentManager.beginTransaction()) { | |
var fragmentA = FragmentA() | |
replace(R.id.container, fragmentA) | |
commit() | |
} | |
return@setOnNavigationItemSelectedListener true | |
} | |
R.id.tab2 -> { | |
with(supportFragmentManager.beginTransaction()) { | |
var fragmentB = FragmentB() | |
replace(R.id.container, fragmentB) | |
commit() | |
} | |
return@setOnNavigationItemSelectedListener true | |
} | |
R.id.tab3 -> { | |
with(supportFragmentManager.beginTransaction()) { | |
var fragmentC = FragmentC() | |
replace(R.id.container, fragmentC) | |
commit() | |
} | |
return@setOnNavigationItemSelectedListener true | |
} | |
} | |
return@setOnNavigationItemSelectedListener false | |
} | |
} | |
} |
반응형
'코딩 일기장 > Android(Kotlin)' 카테고리의 다른 글
[안드로이드] 파이어베이스에 데이터 저장, 검색 (0) | 2021.05.15 |
---|---|
[안드로이드] 스플래시 만들기 (0) | 2021.05.15 |
[안드로이드] 웹 크롤링(이미지) (2) | 2021.05.14 |
[안드로이드] 웹 크롤링 (0) | 2021.05.14 |
[안드로이드] 안드로이드 XML 데이터 파싱 (0) | 2021.05.14 |