简体   繁体   English

自定义底部导航栏图标

[英]Customize Bottom Navigation Bar icons

I use the latest version of the material design Bottom Navigation Bar. 我使用最新版本的材料设计底部导航栏。 I want to have a red dot on one if the icons. 如果要使用图标,我想在上面加一个红点。 Unfortunately, by default Nab bar applies color automatically and red dot color is lost. 不幸的是,默认情况下,导航栏会自动应用颜色,并且会丢失红点颜色。 How to add red dot to an icon? 如何在图标上添加红点? To apply another ViewGroup on top of the Nav bar? 要在导航栏顶部应用另一个ViewGroup? Extend Nav bar class and change the coloring behavior? 扩展导航栏类并更改着色行为? 在此处输入图片说明

this icon is called badge and it is implemented in several ways: Display badge on top of bottom navigation bar's icon 此图标称为badge ,它有几种实现方式: 在底部导航栏图标顶部显示徽章

I have implemented this feature myself this way: 我自己通过以下方式实现了此功能:

private fun addNewRedDotImage() {
    val activityItemView: BottomNavigationItemView = navigation.findViewById(R.id.activity)
    val icon = activityItemView.findViewById<ImageView>(R.id.icon)
    val badge = LayoutInflater
            .from(this)
            .inflate(R.layout.red_dot_notification_badge_layout, activityItemView, false)
    /* get position of icon relative to its parent */
    val iconRect = Rect()
    icon.getLocalVisibleRect(iconRect)
    val badgeLeftPosition = activityItemView.width / 2
    val badgeTopPosition = iconRect.top

    /* set position of badge relative to the icon */
    val params = LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
    params.leftMargin = badgeLeftPosition
    params.bottomMargin = badgeTopPosition
    activityItemView.addView(badge, params)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM