How to set notification count for Android BottomNavigationView?

前端 未结 4 1464
礼貌的吻别
礼貌的吻别 2021-02-03 12:59

I am using

 

to set BottomNavigationView in my android app. My question is

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-03 13:28

    First you need to add app:actionLayout in menu file's item where you want to show badge of your BottomNavigationView like this

    
    

    where unread_message_layout is the layout which has badge something like below

    Here is the XML of that layout

    
    
        
    
    

    and in your activity where BottomNavigationView is available, add this to show badges

    var mbottomNavigationMenuView = binding.bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
    var chatBadge = LayoutInflater.from(this).inflate(R.layout.unread_message_layout,
                    mbottomNavigationMenuView, false)
    var itemView = mbottomNavigationMenuView.getChildAt(2) as BottomNavigationItemView
    
    val tvUnreadChats = chatBadge.findViewById(R.id.tvUnreadChats) as TextView
    tvUnreadChats.text = "1"//String that you want to show in badge
    itemView.addView(chatBadge)
    

    Here getChildAt(2) is third item in my BottomNavigationView

提交回复
热议问题