Android 使用badgeView在表格布局中的选项卡图标上添加徽章

Android 使用badgeView在表格布局中的选项卡图标上添加徽章,android,badge,Android,Badge,我已经看了所有关于如何在选项卡布局中的选项卡图标中添加徽章的问题,但没有答案 这是我的密码 //Get reference to your Tablayout TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(mViewPager); tabLayout.getTabAt(0).setIcon(ICONS[0]); tabLayout.g

我已经看了所有关于如何在选项卡布局中的选项卡图标中添加徽章的问题,但没有答案

这是我的密码

//Get reference to your Tablayout
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
     tabLayout.getTabAt(0).setIcon(ICONS[0]);
    tabLayout.getTabAt(1).setIcon(ICONS[1]);
    tabLayout.getTabAt(2).setIcon(ICONS[2]);

    BadgeView badge = new BadgeView(this, tabLayout.getTabAt(0).getCustomView());
    badge.setText("1"); //Whatever value you should add
    badge.show();


    BadgeView mMotification = new BadgeView(this, tabLayout.getChildAt(1));
    mMotification.setText("10");
    mMotification.show();

我也尝试过许多其他的选择,但BadgeView似乎只接受视图

您可以为选项卡创建自定义布局,并将它们添加到TableLayout中

这就是我为它所做的

TabLayout.Tab tab = tabLayout.getTabAt(0);
    ImageView imageView = new ImageView(this);
    imageView.setImageResource(R.drawable.ic_notifications);

    tab.setCustomView(imageView);
    BadgeView badge = new BadgeView(this, imageView);
    badge.setText("23");
    badge.setBadgeMargin(25,0);
    badge.show();