build.gradle
implementation "androidx.navigation:navigation-fragment-ktx:2.5.3"
implementation "androidx.navigation:navigation-ui-ktx:2.5.3"
res -> new -> resource file
navigation.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"
app:startDestination="@id/first">
<fragment
android:id="@+id/first"
android:name="com.example.android.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" >
</fragment>
<fragment
android:id="@+id/second"
android:name="com.example.android.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
</fragment>
<fragment
android:id="@+id/third"
android:name="com.example.android.ThirdFragment"
android:label="fragment_third"
tools:layout="@layout/fragment_third" >
</fragment>
<fragment
android:id="@+id/fourth"
android:name="com.example.android.FourthFragment"
android:label="fragment_fourth"
tools:layout="@layout/fragment_fourth" >
</fragment>
</navigation>
res -> new -> resource file
bottom_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/first
android:enabled="true"
android:icon="@drawable/icon"
android:title="First"
/>
<item
android:id="@+id/second"
android:enabled="true"
android:icon="@drawable/icon2"
android:title="Second"
/>
<item
android:id="@+id/third"
android:enabled="true"
android:icon="@drawable/icon3"
android:title="Third"
/>
<item
android:id="@+id/fourth"
android:enabled="true"
android:icon="@drawable/icon4"
android:title="Fourth"
/>
</menu>
activity_main.xml
<?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"
tools:context=".activity.MainActivity">
<fragment
android:id="@+id/main_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_bar"
android:layout_width="match_parent"
android:layout_height="60dp"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/main_fragment"
app:menu="@menu/bottom_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navController = findNavController(R.id.main_fragment)
binding.navBar.setupWithNavController(navController)
}
}
메뉴 클릭 색 변경 방법
activity_main.xml에서 BottomNavigationView에 다음 속성 추가
app:itemIconTint="@drawable/bottom_nav_selector" //아이콘 색상
app:itemTextColor="@drawable/bottom_nav_selector" //글씨 색상
drawable/bottom_nav_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/light_purple" android:state_checked="true"/>
<item android:color="@color/gray" android:state_checked="false"/>
</selector>
선택한 메뉴는 light_purple
미선택 메뉴는 gray 색상으로 나타난다.
'안드로이드 > Jetpack' 카테고리의 다른 글
[안드로이드] Jetpack (0) | 2023.11.24 |
---|---|
[안드로이드] ViewModel (1) | 2023.11.24 |
[안드로이드] LiveData (1) | 2023.11.23 |
[안드로이드] Databinding (1) | 2023.11.23 |
[안드로이드] Room DB (0) | 2023.11.23 |