2017-02-21 20 views
11

我一直在尝试在API 25中发布BottomNavigationView。我想在底部导航栏中的某个菜单项上显示通知标志(例如,带有或不带有计数的小蓝色圆圈)。有没有办法在API 25中引入的谷歌官方BottomNavigationView菜单项上显示通知标志?

我有一个选择器可绘制的地方,我添加了检查真实和检查错误状态与灰色绘制其上有一个蓝点。当用户导航到其他导航项目时,整个菜单按钮变成灰色,并且徽章也变为灰色。我知道这是因为itemIconTint应用于drawable,这是因为图标的一部分不能使用,所以具有不同颜色的徽章。有没有其他方法可以实现这一目标?

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/bottom_navigation" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="@color/white" 
    app:itemIconTint="@color/selector_bottom_nav" 
    app:itemTextColor="@color/selector_bottom_nav" 
    android:layout_gravity="bottom" 
    app:menu="@menu/menu_bottom_nav"> 
</android.support.design.widget.BottomNavigationView> 

这就是我如何使用它。删除itemIconTint并以编程方式更改图标绘制不起作用。

在Android开发人员上我什么都没发现,它也很新,网上也没有东西可用。

有底部导航栏的自定义库,但我正在寻找其官方支持。

任何想法,任何人?

+0

检查该库https://github.com/volders/Badger – myatmins

回答

3

回答我的问题:

我最后决定将2 ImageViews与徽章可绘制和可见性GONE在我的布局。我计算徽章的定位是这样的:

private void calculateBadgesPosition() { 
int totalNavItems = 3; 
     for(int i = 0 ; i < bottomNavigation.getChildCount() ; i++) { 
      View view = bottomNavigation.getChildAt(i); 

// Since Y remains same for all badges at least in my case. 
// 1.3 is a factor that fits my placement of badge. 
      double y = getResources().getDisplayMetrics().heightPixels - (view.getMeasuredHeight() * 1.3); 
      if(image2ndItemBadge != null) { 
       float x = getResources().getDisplayMetrics().widthPixels/2 + imageClassroomBadge.getMeasuredWidth()/2; 

       image2ndItemBadge.setX(x); 
       image2ndItemBadge.setY((float)y); 
       image2ndItemBadge.requestLayout(); 
      } 
// Since BottomNavigationView items are equally distributed you can find 
// the right position for 3rd item (in my case last item) by dividing 
// BottomNavigationView width by 3 and then by 2 to get the middle of that 
// item, which was needed in my case. 
      if(image3rdItemBadge != null) { 
       float x = getResources().getDisplayMetrics().widthPixels - ((view.getMeasuredWidth()/totalNavItems)/2); 

       image3rdItemBadge.setX(x); 
       image3rdItemBadge.setY((float)y); 
       image3rdItemBadge.requestLayout(); 
      } 
      // BottomNavigationView count should always be 1 
      // but I observed the count was 2 in API 19. So I break after first iteration. 
      break; 
     } 
    } 

您可以删除for循环,只是检查,如果孩子数大于0,并得到那个孩子。我称上述方法在的onCreate的这样的端:

ViewTreeObserver viewTreeObserver = getWindow().getDecorView().getViewTreeObserver(); 
      viewTreeObservier.addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener() { 
       @Override 
       public void onGlobalLayout() { 
        calculateBadgesPosition(); 
        getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this); 
       } 
      }); 

徽章ImageViewBottomNavigationView项目不是可绘部分将不会被BottomNavigationView适用于项的选择/未选择该着色来实现。此外,如果你看到你的徽章没有出现,请尝试给它更高的elevation 8或16或其他。在我的情况下,我的徽章留在BottomNavigationView后面可能是因为它有更高的海拔或Z指数。

我使用3种不同的屏幕尺寸测试了它,并且所有的定位都是相同的。

我希望这可以帮助任何人遇到与官方BottomNavigationView类似的问题。

此外,如果你有更好的方法,请分享它。

+1

必须这样做,但感谢分享它。它为我工作! –

0

实现此目的的一种方法是对每个项目使用两个图标 - 一个使用徽章,另一个不使用并以编程方式替换它们。或者,也可以使用单个图标而不是两个图标,并以编程方式绘制徽章。因此,代码可以是这个样子(假设你知道每个项目的指标,可以得到Drawable每个):

public static void updateItem(BottomNavigationView bottomNavigationView, int index, Drawable icon) { 
    Menu menu = bottomNavigationView.getMenu(); 
    MenuItem item = menu.getItem(index); 

    if (item != null) { 
     item.setIcon(icon); 
    } 
} 
+0

感谢您的回答。 就像我在问题中提到的那样,以编程方式更改drawable并没有帮助。 我已经有了不同的drawable,但只要用户单击BottomNavigationView中的其他菜单项,灰色色调就会应用于drawable。显示该项目不再被选中。该色调默认应用于整个图像,因此徽章也会变灰。 – Waqas

0

使用Textview创建布局。

通过为BottomNavigationView添加BottomNavigationMenuView作为子视图来使视图膨胀。将计数添加到所需菜单。

请参阅下面的链接。

https://stackoverflow.com/a/48269868/4675067

相关问题