1. 摘要
2. 使用方法
3. BottomNavigationView取消內建顏色變化
1. 摘要
參考:Navigation使用官網、Android Jetpack Navigation與Safe Args使用
Library引用方法,以及基本的設定,請參考Android Jetpack Navigation與Safe Args使用
這裡主要是介紹「Navigation搭配BottomNavigationView使用方法」
2. 使用方法
在Layout中新增
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?attr/colorPrimaryVariant"
app:labelVisibilityMode="unlabeled"
app:itemIconSize="@dimen/icon_45"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/local_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
onCreate新增
val navController = findNavController(activity, R.id.nav_host_fragment_activity_main)
定義AppBarConfiguration
以下的R.id是BottomNavigationView的menu,menu/bottom_nav_menu裡面的
val appBarConfiguration = AppBarConfiguration(setOf( R.id.navigation_home, R.id.navigation_manager, R.id.navigation_account, R.id.navigation_data ))
設定appBarConfiguration
setupActionBarWithNavController(navController, appBarConfiguration) setupWithNavController(navController)
點擊BottomNavigationView監聽方式
navController.addOnDestinationChangedListener { _, destination, _ -> when (destination.id) { R.id.navigation_home -> { } R.id.navigation_manager -> { } R.id.navigation_account -> { } R.id.navigation_data -> { } } }
3. BottomNavigationView取消內建顏色變化
bottomNavigationView.itemIconTintList = null
這個方法目前無法於xml中設定
設定後,BottomNavigationView Menu就可以使用比較複雜的icon
相關文章
Android Jetpack Navigation與Safe Args使用 | |
1. 摘要 2. 新增Navigation依賴 3. 基本使用方法 4. 使用Navigation Safe Args |
訂閱Codeilin的旅程,若有最新消息會通知。
廣告