如何在 Android 中将徽章添加到 BottomNavigationView
How to add badge to BottomNavigationView in Android
我想使用 BottomNavigationView
视图来设置一些 fragments
!
但我想将 徽章(带文字的圆圈背景)设置为 BottomNavigationView
中的一项!
对于这项工作,我编写了以下代码。
在我的代码中,它第一次起作用。
但是当更新 徽章文本 时,不显示徽章文本只显示 圆形背景 而没有任何文本!!
为什么更新徽章编号时隐藏文本计数?!
我的activity代码:
class MainActivity : AppCompatActivity() {
private lateinit var disposable: Disposable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setupNavigation()
bottomNavigationView.selectedItemId = R.id.tab
disposable = RxBus.listen(RxEvent.EventAddPerson::class.java).subscribe {
badgeBottomNavBar(it.personName.toInt())
}
}
private fun setupNavigation() {
val navController = Navigation.findNavController(this, R.id.mainNavigationFragment)
//setupActionBarWithNavController(this, navController)
NavigationUI.setupWithNavController(bottomNavigationView, navController)
}
override fun onSupportNavigateUp() = Navigation.findNavController(this, R.id.mainNavigationFragment).navigateUp()
private fun badgeBottomNavBar(count: Int) {
val bottomNavigationMenuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
val v = bottomNavigationMenuView.getChildAt(2)
val itemView = v as BottomNavigationItemView
val view = LayoutInflater.from(this).inflate(R.layout.notification_test, itemView, true)
val badgeText: TextView = view.findViewById(R.id.notificationsBadge)
badgeText.text = count.toString()
}
override fun onDestroy() {
super.onDestroy()
if (!disposable.isDisposed) disposable.dispose()
}
}
片段代码:
class NotificationDetails : Fragment() {
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_notification_details, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
arguments?.let {
val passedArguments = NotificationDetailsArgs.fromBundle(it)
notificationDetailsLabel.text = passedArguments.notificationId
}
notificationDetailsLabel.setOnClickListener {
RxBus.publish(RxEvent.EventAddPerson(editText.text.toString()))
}
}
}
我该如何解决?
朋友你的问题很简单!
您使用 bottomNavBar
选项和 locally 变量!
首先你应该全局初始化这个变量然后使用它!
例如下面的代码:
class MainActivity : AppCompatActivity() {
private lateinit var disposable: Disposable
private lateinit var bottomNavigationMenuView: BottomNavigationMenuView
private lateinit var itemView: BottomNavigationItemView
private lateinit var view: View
private lateinit var badgeText: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setupNavigation()
bottomNavigationView.selectedItemId = R.id.tab5
bottomNavigationMenuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
val v = bottomNavigationMenuView.getChildAt(2)
itemView = v as BottomNavigationItemView
view = LayoutInflater.from(this).inflate(R.layout.notification_test, itemView, true)
badgeText = view.findViewById(R.id.notificationsBadge)
//Set badge
badgeBottomNavBar("15")
disposable = RxBus.listen(RxEvent.EventAddPerson::class.java).subscribe {
badgeBottomNavBar(it.personName)
}
}
private fun setupNavigation() {
val navController = Navigation.findNavController(this, R.id.mainNavigationFragment)
NavigationUI.setupWithNavController(bottomNavigationView, navController)
}
override fun onSupportNavigateUp() = Navigation.findNavController(this, R.id.mainNavigationFragment).navigateUp()
private fun badgeBottomNavBar(count: String) {
badgeText.text = count
}
override fun onDestroy() {
super.onDestroy()
if (!disposable.isDisposed) disposable.dispose()
}
}
希望对你有帮助
我想使用 BottomNavigationView
视图来设置一些 fragments
!
但我想将 徽章(带文字的圆圈背景)设置为 BottomNavigationView
中的一项!
对于这项工作,我编写了以下代码。
在我的代码中,它第一次起作用。
但是当更新 徽章文本 时,不显示徽章文本只显示 圆形背景 而没有任何文本!!
为什么更新徽章编号时隐藏文本计数?!
我的activity代码:
class MainActivity : AppCompatActivity() {
private lateinit var disposable: Disposable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setupNavigation()
bottomNavigationView.selectedItemId = R.id.tab
disposable = RxBus.listen(RxEvent.EventAddPerson::class.java).subscribe {
badgeBottomNavBar(it.personName.toInt())
}
}
private fun setupNavigation() {
val navController = Navigation.findNavController(this, R.id.mainNavigationFragment)
//setupActionBarWithNavController(this, navController)
NavigationUI.setupWithNavController(bottomNavigationView, navController)
}
override fun onSupportNavigateUp() = Navigation.findNavController(this, R.id.mainNavigationFragment).navigateUp()
private fun badgeBottomNavBar(count: Int) {
val bottomNavigationMenuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
val v = bottomNavigationMenuView.getChildAt(2)
val itemView = v as BottomNavigationItemView
val view = LayoutInflater.from(this).inflate(R.layout.notification_test, itemView, true)
val badgeText: TextView = view.findViewById(R.id.notificationsBadge)
badgeText.text = count.toString()
}
override fun onDestroy() {
super.onDestroy()
if (!disposable.isDisposed) disposable.dispose()
}
}
片段代码:
class NotificationDetails : Fragment() {
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_notification_details, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
arguments?.let {
val passedArguments = NotificationDetailsArgs.fromBundle(it)
notificationDetailsLabel.text = passedArguments.notificationId
}
notificationDetailsLabel.setOnClickListener {
RxBus.publish(RxEvent.EventAddPerson(editText.text.toString()))
}
}
}
我该如何解决?
朋友你的问题很简单!
您使用 bottomNavBar
选项和 locally 变量!
首先你应该全局初始化这个变量然后使用它!
例如下面的代码:
class MainActivity : AppCompatActivity() {
private lateinit var disposable: Disposable
private lateinit var bottomNavigationMenuView: BottomNavigationMenuView
private lateinit var itemView: BottomNavigationItemView
private lateinit var view: View
private lateinit var badgeText: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setupNavigation()
bottomNavigationView.selectedItemId = R.id.tab5
bottomNavigationMenuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
val v = bottomNavigationMenuView.getChildAt(2)
itemView = v as BottomNavigationItemView
view = LayoutInflater.from(this).inflate(R.layout.notification_test, itemView, true)
badgeText = view.findViewById(R.id.notificationsBadge)
//Set badge
badgeBottomNavBar("15")
disposable = RxBus.listen(RxEvent.EventAddPerson::class.java).subscribe {
badgeBottomNavBar(it.personName)
}
}
private fun setupNavigation() {
val navController = Navigation.findNavController(this, R.id.mainNavigationFragment)
NavigationUI.setupWithNavController(bottomNavigationView, navController)
}
override fun onSupportNavigateUp() = Navigation.findNavController(this, R.id.mainNavigationFragment).navigateUp()
private fun badgeBottomNavBar(count: String) {
badgeText.text = count
}
override fun onDestroy() {
super.onDestroy()
if (!disposable.isDisposed) disposable.dispose()
}
}
希望对你有帮助