2023.7.24~25
1. ์ค๋น
1) ๋ฉ์ธ ์กํฐ๋นํฐ
- activity_main.xml / MainActivity.kt
2) ๋ค๋น๊ฒ์ด์ ๋ฐ ๋ฉ๋ด (res > ์ฐํด๋ฆญ new > Android Resource Derectory, menu ์ถ๊ฐ / menu resource file๋ก ์์ฑ)
- bottom_nav.xml
3) ํ๋๊ทธ๋จผํธ(๋ค๋น๊ฒ์ด์ ๋ฐ๋ฅผ ์ด์ฉํ์ฌ ํ๋ฉด ์ ํ์ ์ํด ํ์ํ ๋งํผ์ ํ๋๊ทธ๋จผํธ๋ฅผ ๋ง๋ค์ด์ฃผ๋ฉด ๋จ)
- fragment_asmr.xml / AsmrFragment.kt
- fragment_book_search.xml / BookSearchFragment.kt
- fragment_home.xml / HomeFragment.kt
- fragment_book_shelf.xml / BookShelfFragment.kt
- fragment_my_page.xml / MyPageFragment.kt
2. xml ์์ฑ
- bottom_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="ASMR"
android:id="@+id/asmr"
android:icon="@drawable/bottom_asmricon" />
<item android:title="์ฑ
๊ฒ์"
android:id="@+id/search"
android:icon="@drawable/bottom_searchicon" />
<item android:title="์บ๋ฆญํฐ"
android:id="@+id/home"
android:icon="@drawable/bottom_homeicon" />
<item android:title="์ฑ
์ฅ"
android:id="@+id/shelf"
android:icon="@drawable/bottom_bookshelficon" />
<item android:title="๋ง์ดํ์ด์ง"
android:id="@+id/mypage"
android:icon="@drawable/bottom_mypageicon" />
</menu>
- activity_main.xml
- @color/bottomNavColor์ ๊ฒฝ์ฐ #00000000๋ก ํฌ๋ช ํ ์์์ด๋ค.
- ์ด ํ๋ก์ ํธ์์ ๋ค๋น๊ฒ์ด์
๋ฐ๊ฐ ํฌ๋ช
ํ์ฌ ๋ค์ ๋ฐฐ๊ฒฝ์ด๋ฏธ์ง๊ฐ ๋ณด์ฌ์ผํ๋ค.
- ๋ฐ๋ฅผ ํฌ๋ช
ํ๊ฒ ํ๋ฉด ๊ฒฝ๊ณ์ ์ด ๋ณด์ด๋๋ฐ ์ด๊ฒ๋ ํฌ๋ช
ํ๊ฒ ๋ฐ๊ฟ์ฃผ์๋ค. (outlineSpotShadowColor, outlineAmbientShadowColor)
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNav"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNav"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/bottomNavColor"
android:outlineSpotShadowColor="@color/bottomNavColor"
android:outlineAmbientShadowColor="@color/bottomNavColor"
app:itemRippleColor="@color/bottomNavColor"
app:itemTextColor="#666493"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav"/>
</androidx.constraintlayout.widget.ConstraintLayout>
- fragment์ xml์ ์๋ต
2. xml ์์ฑ
- ๋ค๋น๊ฒ์ด์ ๋ฐ์ ์์ด์ฝ์ด ์๋ ์ด๋ฏธ์ง๋ฅผ ๋ฃ์ผ๋, ์ด๋ฏธ์ง์ ์์์ด ๋์ค์ง ์๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
bottomNav.setItemIconTintList(null)
์ด ์ฝ๋๋ฅผ ์์ฑํด ์ฃผ์๋ค. (์๋์ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.)
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// ๋ฐํ
๋ค๋น๊ฒ์ด์
์ด๋ ๊ตฌํ
var bottomNav = findViewById<BottomNavigationView>(R.id.bottomNav)
bottomNav.setItemIconTintList(null); // ์์ด์ฝ ์ด๋ฏธ์ง ์์ ๋ณด์ฌ์ค
// ๋ค๋น๊ฒ์ด์
๋ฐ ๋ฒํผ์ ๋ฐ๋ฅธ ํ๋๊ทธ๋จผํธ ์ด๋
bottomNav.run {
setOnItemSelectedListener { item ->
when(item.itemId) {
R.id.asmr -> { // asmr
changeFragment(AsmrFragment())
}
R.id.search -> { // ์ฑ
๊ฒ์
changeFragment(BookSearchFragment.newInstance(userEmail))
}
R.id.home -> { // ์บ๋ฆญํฐ(ํ)
changeFragment(HomeFragment.newInstance(userEmail))
}
R.id.shelf -> { // ์ฑ
์ฅ
changeFragment(BookShelfFragment.newInstance(userEmail))
}
R.id.mypage -> { // ๋ง์ดํ์ด์ง
changeFragment(MyPageFragment.newInstance(userEmail))
}
}
true
}
}
}
// ํ๋๊ทธ๋จผํธ ์ด๋ ํจ์
private fun changeFragment(fragment: Fragment) {
supportFragmentManager
.beginTransaction()
.replace(R.id.frameLayout, fragment)
.commit()
}
}
๊ฒฐ๊ณผ
(์คํ๋์๋ ์ง๋ ๊ธ์ ์ฐธ๊ณ ํ์)