1、废话不多,直接上代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
supportActionBar?.hide()
val navView: BottomNavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_activity_main)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
@SuppressLint("ResourceType")
val colorState: ColorStateList = resources.getColorStateList(
R.drawable.ic_notifications_black_24dp,
resources.newTheme()
)
binding.navView.itemTextColor = colorState
binding.ivMenu.setOnClickListener {
showSetDialog()
}
}
supportActionBar?.hide(),注意切勿使用或改动清单文件中的android:theme 属性值。
binding.navView.itemTextColor = colorState
android:icon 属性值。属性值尽量实用 drawable资源,可通过Asset 创建自己项目需要的 drawable资源。 也可以直接使用 mipmap资源。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/navigation_home"
android:icon="@drawable/round_w_g"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/round_w_g"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/round_w_g"
android:title="@string/title_notifications" />
</menu>
如有问题可联系博主。? 谢谢阅读,烦请关注!!