๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Study/Android

[์•ˆ๋“œ๋กœ์ด๋“œ ์ฝ”ํ‹€๋ฆฐ] ํ”„๋กœ์ ํŠธ - ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์ด์šฉํ•œ ๋„ค๋น„๊ฒŒ์ด์…˜๋ฐ”

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)

์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์—ˆ๋‹ค. (์•„๋ž˜์˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.)

-  https://stackoverflow.com/questions/57861254/how-to-change-a-specific-icon-image-from-bottom-navigation-view 

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()
    }
}

 

๊ฒฐ๊ณผ

(์Šคํ”Œ๋ž˜์‹œ๋Š” ์ง€๋‚œ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์ž)